如何在VB.NET中创建一个通用属性? [英] How do I create a generic property in VB.NET?
本文介绍了如何在VB.NET中创建一个通用属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想这样做:
I'd like to do something like this:
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
基本上,我想要可能是任何类型的项目的集合。然后,我可以这样做:
Basically, I want to have a collection of items that may be of any type. Then, I'll be able to do something like this:
Dim myPropertyValue as <the type of some value>
if (MyProperty.Contains(<some value>))
myPropertyValue = CType(MyProperty(<some value>), <the type of some value>)
我该怎么做?还是有更好的方法比使用泛型?
How can I do this? Or is there a better way than using a generic type?
推荐答案
您可能需要创建一个泛型类来执行此操作
You may have to create a generic class to do this
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
这篇关于如何在VB.NET中创建一个通用属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文