[英]How do I create a generic property in VB.NET?
我想做這樣的事情:
Private _myCollection As IList(Of T)
Public Property MyProperty(Of T)() as IList(Of T)
Get
Return Me._myCollection
End Get
Set(ByVal value As String)
Me._myCollection = value
End Set
End Property
基本上,我想擁有一系列可能屬於任何類型的物品。 然后,我將能夠做到這樣的事情:
Dim myPropertyValue as <the type of some value>
if (MyProperty.Contains(<some value>))
myPropertyValue = CType(MyProperty(<some value>), <the type of some value>)
我怎樣才能做到這一點? 或者有比使用泛型類型更好的方法嗎?
您可能必須創建一個通用類來執行此操作
Public Class MyClass(Of T)
Private _myCollection As IList(Of T)
Public Property MyProperty() as IList(Of T)
Get
Return Me._myCollection
End Get
Set(ByVal value As String)
Me._myCollection = value
End Set
End Property
End Class
如果您不想將整個類轉換為通用類,您還可以向類中添加通用過程(請參閱MSDN ):
Public Function GetMyProperty(Of T) As T
....
End Function
Public Sub SetMyProperty(value as T)
...
End Sub
但它並不像物業那么優雅。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.