Community discussion forum

Generics and System.Type

Tags: India
  • 5 months ago

    In the code below, I am trying to create a collection of "SomeClass" objects, each one being created based on the generic type chosen by the caller of "CreateSomeList", like this:

    Dim c as Collection = CreateSomeList(GetType(Integer), GetType(Single), GetType(Double), GetType(Date)) 

    The problem is that the line "Dim sc as SomeClass(Of t)" fails to compile, saying "Type 't' is not defined.".

    Here is the complete code to reproduce:

    Public Function CreateSomeList(ByVal ParamArray Types() As System.Type) As Collection

    Dim somelist As New Collection For Each t As System.Type In Types
        Dim sc As SomeClass(Of t) ' this is the line that fails to compile
        somelist.Add(sc)
    Next

    Return somelist
    End Function

    Public Class SomeClass(Of T)
    Private mValue As T
    Public Property Value() As T
        Get
            Return mValue
        End Get
        Set(ByVal value As T)
            mValue = value
        End Set
    End Property

    End Class

    How can I make this work?

Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).