我正在寻找一个小专家的设计见解。我试图从一个泛型类保存一个重载的属性。Vb.Net泛型与重载事件混合
基类
Public MustInherit Class BaseEvent
Public MustOverride ReadOnly Property IsWorkCalendar() As Boolean
Private _Location As Enums.LocationType
Public Overridable Property Location() As Enums.LocationType
Get
Return _Location
End Get
Set(ByVal value As Enums.LocationType)
_Location = value
End Set
End Property
End Class
实现BaseEvent类
Public Class MyEvent
Inherits BaseEvent
Private _Location As String
Public Overloads Property Location As String
Get
Return _Location
End Get
Set(value As String)
_Location = value
End Set
End Property
End Class
泛型类
Public Function GetItemHeaders(Of T As {Core.Events.BaseEvent, New})() As IEnumerable(Of Core.Events.BaseEvent) Implements IMethods.GetItemHeaders
Dim myEvents = Helper.GetAllEvents(_Service)
Dim genericEvents As New List(Of BaseEvent)()
...loop through items...
Dim genericEvent As T = New T()
If genericEvent.IsWorkCalendar Then
Dim location As Enums.LocationType = Enums.LocationType.NotConfigured
If ([Enum].IsDefined(GetType(Enums.LocationType), fooString)) Then
location = [Enum].Parse(GetType(Enums.LocationType), fooString)
End If
genericEvent.Location = location
Else
- Always uses the BaseEvent Location and casses an error since I am trying to store a string
genericEvent.Location = otherPlace
End If
....
genericEvents.Add(genericEvent)
Next
Return genericEvents
End Function
提前感谢! Ryan
很高兴看到人们使用VB进行OOP透视,而不是人们仍然持有的VB6方式。荣誉给你! – Jeremy 2011-05-24 22:08:37
谢谢,我是一个VB.Net/C#程序员。这项新工作全部都是VB,所以它的时间让我的大脑再次切换模式。 – Ryan 2011-05-25 14:16:19