Use a System.Type in Generics?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cmay

    #1

    Use a System.Type in Generics?

    Is this something you are not allowed to do?

    I basically want to create an instance of a generic class where the
    type T is not know at compile time.

    e.g.

    dim s as System.Type = GetMyType()
    dim o as MyObj(Of s)


    I have not found a way to do this, so I am guessing it is not possible.

  • Herfried K. Wagner [MVP]

    #2
    Re: Use a System.Type in Generics?

    "cmay" <cmay@walshgrou p.com> schrieb:[color=blue]
    > I basically want to create an instance of a generic class where the
    > type T is not know at compile time.
    >
    > e.g.
    >
    > dim s as System.Type = GetMyType()
    > dim o as MyObj(Of s)[/color]

    \\\
    Dim GenericType As Type = GetType(List(Of ))
    Dim ParameterTypes( ) As Type = {GetType(Intege r)}
    Dim o As Object = _
    Activator.Creat eInstance( _
    GenericType.Mak eGenericType(Pa rameterTypes) _
    )
    ///

    --
    M S Herfried K. Wagner
    M V P <URL:http://dotnet.mvps.org/>
    V B <URL:http://classicvb.org/petition/>

    Comment

    • cmay

      #3
      Re: Use a System.Type in Generics?

      Thanks HKW!


      Herfried K. Wagner [MVP] wrote:[color=blue]
      > "cmay" <cmay@walshgrou p.com> schrieb:[color=green]
      > > I basically want to create an instance of a generic class where the
      > > type T is not know at compile time.
      > >
      > > e.g.
      > >
      > > dim s as System.Type = GetMyType()
      > > dim o as MyObj(Of s)[/color]
      >
      > \\\
      > Dim GenericType As Type = GetType(List(Of ))
      > Dim ParameterTypes( ) As Type = {GetType(Intege r)}
      > Dim o As Object = _
      > Activator.Creat eInstance( _
      > GenericType.Mak eGenericType(Pa rameterTypes) _
      > )
      > ///
      >
      > --
      > M S Herfried K. Wagner
      > M V P <URL:http://dotnet.mvps.org/>
      > V B <URL:http://classicvb.org/petition/>[/color]

      Comment

      • Cor Ligthert [MVP]

        #4
        Re: Use a System.Type in Generics?

        Herfried,

        I am curious how I can use this Generic List of a typed class, while I don't
        know the members of the class?

        Can you give me an example?

        Cor

        "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> schreef in bericht
        news:OfAidWNiGH A.1208@TK2MSFTN GP02.phx.gbl...[color=blue]
        > "cmay" <cmay@walshgrou p.com> schrieb:[color=green]
        >> I basically want to create an instance of a generic class where the
        >> type T is not know at compile time.
        >>
        >> e.g.
        >>
        >> dim s as System.Type = GetMyType()
        >> dim o as MyObj(Of s)[/color]
        >
        > \\\
        > Dim GenericType As Type = GetType(List(Of ))
        > Dim ParameterTypes( ) As Type = {GetType(Intege r)}
        > Dim o As Object = _
        > Activator.Creat eInstance( _
        > GenericType.Mak eGenericType(Pa rameterTypes) _
        > )
        > ///
        >
        > --
        > M S Herfried K. Wagner
        > M V P <URL:http://dotnet.mvps.org/>
        > V B <URL:http://classicvb.org/petition/>[/color]


        Comment

        • Herfried K. Wagner [MVP]

          #5
          Re: Use a System.Type in Generics?

          "Cor Ligthert [MVP]" <notmyfirstname @planet.nl> schrieb:[color=blue]
          > I am curious how I can use this Generic List of a typed class, while I
          > don't know the members of the class?[/color]

          The problem is that you can only do that using reflection because 'List(Of
          X)' is not a 'List(Of )'. Thus I do not think that the solution I posted
          makes much sense because generics are mainly a compile-time feature and the
          compile-time advantages are lost when using reflection.

          --
          M S Herfried K. Wagner
          M V P <URL:http://dotnet.mvps.org/>
          V B <URL:http://classicvb.org/petition/>

          Comment

          Working...