How do I create a generic property in VB.NET?

前端 未结 2 618
刺人心
刺人心 2021-01-13 18:41

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._myColl         


        
相关标签:
2条回答
  • 2021-01-13 19:01

    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
    
    0 讨论(0)
  • 2021-01-13 19:11

    If you do not want to convert your whole class to a generic one you could also add generic procedures to your class (see MSDN):

    Public Function GetMyProperty(Of T) As T
       ....
    End Function
    Public Sub SetMyProperty(value as T)
       ...
    End Sub
    

    But it is not as elegant as the properties.

    0 讨论(0)
提交回复
热议问题