【发布时间】:2011-05-24 20:59:05
【问题描述】:
我正在寻找一些专业的设计见解。我正在尝试从泛型类中保存重载属性。
基类
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
提前致谢! 瑞恩
【问题讨论】:
-
很高兴看到人们使用带有 OOP 视角的 VB,而不是人们仍然坚持的 VB6 方式。向你致敬!
-
谢谢,我是一名 VB.Net/C# 程序员。这个新工作全是vb,所以我的大脑又该切换模式了。
标签: vb.net generics polymorphism overloading