如何使用泛型方法替换重复的属性?(How to use generic method to replace duplicated properties?)

编程入门 行业动态 更新时间:2024-10-27 00:25:52
如何使用泛型方法替换重复的属性?(How to use generic method to replace duplicated properties?)

目前我的代码具有以下属性:

Private _queueRetriever As Services.IQueueRetriever Public ReadOnly Property QueueRetriever As Services.IQueueRetriever Get If _queueRetriever Is Nothing Then RemotingSetup.RegisterHttpBinaryChannel() _queueRetriever = RemotingFactory.CreateProxy(Of Services.IQueueRetriever)(Options.DevLocal) End If Return _queueRetriever End Get End Property Private _queueCountRetriever As Services.IQueueCounter Public ReadOnly Property QueueCounter As Services.IQueueCounter Get If _queueCountRetriever Is Nothing Then RemotingSetup.RegisterHttpBinaryChannel() _queueCountRetriever = RemotingFactory.CreateProxy(Of Services.IQueueCounter)(Options.DevLocal) End If Return _queueCountRetriever End Get End Property

如您所见,两个属性的代码基本相同。 只有类型不同。 我在泛型方法方面不是很有经验,但我觉得我应该能够使用泛型使这段代码更干 。 可以这样做,还是这不是通用方法的工作?

编辑

在Marc的回答基础上,我想出了以下内容:

Protected Services As New Dictionary(Of Type, Object) Protected Function CreateService(Of T)() As T If Not Services.ContainsKey(GetType(T)) Then RemotingSetup.RegisterHttpBinaryChannel() Services.Add(GetType(T), RemotingFactory.CreateProxy(Of T)(Options.DevLocal)) End If Return DirectCast(Services(GetType(T)), T) End Function

当然,这可以像这样使用:

Me.CreateService(Of IService).Foo()

Currently my code has the following properties:

Private _queueRetriever As Services.IQueueRetriever Public ReadOnly Property QueueRetriever As Services.IQueueRetriever Get If _queueRetriever Is Nothing Then RemotingSetup.RegisterHttpBinaryChannel() _queueRetriever = RemotingFactory.CreateProxy(Of Services.IQueueRetriever)(Options.DevLocal) End If Return _queueRetriever End Get End Property Private _queueCountRetriever As Services.IQueueCounter Public ReadOnly Property QueueCounter As Services.IQueueCounter Get If _queueCountRetriever Is Nothing Then RemotingSetup.RegisterHttpBinaryChannel() _queueCountRetriever = RemotingFactory.CreateProxy(Of Services.IQueueCounter)(Options.DevLocal) End If Return _queueCountRetriever End Get End Property

As you can see, the code is essentially the same for both properties. Only the types are different. I'm not very experienced in generic methods, but I feel like I should be able to make this code more DRY using generics. Can it be done or is this not a job for generic methods?

EDIT

Building on Marc's answer, I came up with the following:

Protected Services As New Dictionary(Of Type, Object) Protected Function CreateService(Of T)() As T If Not Services.ContainsKey(GetType(T)) Then RemotingSetup.RegisterHttpBinaryChannel() Services.Add(GetType(T), RemotingFactory.CreateProxy(Of T)(Options.DevLocal)) End If Return DirectCast(Services(GetType(T)), T) End Function

Which, of course, can be used like so:

Me.CreateService(Of IService).Foo()

最满意答案

你可以做(​​并且转而使用C#道歉,但我的VB技能在这里缺乏细微之处 - 我确信它在VB中工作得很好)

private T GetProxy<T>(ref T field) where T : class { if(field == null) { RemotingSetup.RegisterHttpBinaryChannel(); field = RemotingFactory.CreateProxy<T>(Options.DevLocal); } return field; }

有:

private Services.IQueueCounter queueCounter; public Services.IQueueCounter QueueCounter { get { return GetProxy(ref queueCounter); } } private Services.IQueueRetriever queueRetriever; public Services.IQueueRetriever QueueRetriever { get { return GetProxy(ref queueRetriever); } }

You could do (and apologies for switching into C#, but my VB skills lack the subtlety here - I'm sure it works fine in VB too though)

private T GetProxy<T>(ref T field) where T : class { if(field == null) { RemotingSetup.RegisterHttpBinaryChannel(); field = RemotingFactory.CreateProxy<T>(Options.DevLocal); } return field; }

with:

private Services.IQueueCounter queueCounter; public Services.IQueueCounter QueueCounter { get { return GetProxy(ref queueCounter); } } private Services.IQueueRetriever queueRetriever; public Services.IQueueRetriever QueueRetriever { get { return GetProxy(ref queueRetriever); } }

更多推荐

本文发布于:2023-08-04 02:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1408851.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   属性   方法   generic   properties

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!