当前位置 : 主页 > 编程语言 > c语言 >

如何在VB.NET中创建通用属性?

来源:互联网 收集:自由互联 发布时间:2021-06-24
我想做这样的事情: 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 SetEnd Property 基本上,我想拥有
我想做这样的事情:

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
网友评论