Home All Groups Group Topic Archive Search About

Use a System.Type in Generics?

Author
5 Jun 2006 6:33 PM
cmay
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.

Author
5 Jun 2006 7:24 PM
Herfried K. Wagner [MVP]
"cmay" <c***@walshgroup.com> schrieb:
> 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)

\\\
Dim GenericType As Type = GetType(List(Of ))
Dim ParameterTypes() As Type = {GetType(Integer)}
Dim o As Object = _
    Activator.CreateInstance( _
        GenericType.MakeGenericType(ParameterTypes) _
    )
///

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
5 Jun 2006 8:00 PM
cmay
Thanks HKW!


Herfried K. Wagner [MVP] wrote:
Show quoteHide quote
> "cmay" <c***@walshgroup.com> schrieb:
> > 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)
>
> \\\
> Dim GenericType As Type = GetType(List(Of ))
> Dim ParameterTypes() As Type = {GetType(Integer)}
> Dim o As Object = _
>     Activator.CreateInstance( _
>         GenericType.MakeGenericType(ParameterTypes) _
>     )
> ///
>
> --
>  M S   Herfried K. Wagner
> M V P  <URL:http://dotnet.mvps.org/>
>  V B   <URL:http://classicvb.org/petition/>
Author
6 Jun 2006 5:50 AM
Cor Ligthert [MVP]
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

Show quoteHide quote
"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> schreef in bericht
news:OfAidWNiGHA.1208@TK2MSFTNGP02.phx.gbl...
> "cmay" <c***@walshgroup.com> schrieb:
>> 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)
>
> \\\
> Dim GenericType As Type = GetType(List(Of ))
> Dim ParameterTypes() As Type = {GetType(Integer)}
> Dim o As Object = _
>    Activator.CreateInstance( _
>        GenericType.MakeGenericType(ParameterTypes) _
>    )
> ///
>
> --
> M S   Herfried K. Wagner
> M V P  <URL:http://dotnet.mvps.org/>
> V B   <URL:http://classicvb.org/petition/>
Author
6 Jun 2006 10:36 AM
Herfried K. Wagner [MVP]
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> schrieb:
> I am curious how I can use this Generic List of a typed class, while I
> don't know the members of the class?

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/>