|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
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. "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/> 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/> 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/> "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> schrieb: The problem is that you can only do that using reflection because 'List(Of > I am curious how I can use this Generic List of a typed class, while I > don't know the members of the class? 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/>
Close form if no user action including mouse move over form.
Can't create AutoFiltered Excel worksheet using VB.NET CODE to Test ALPHA Transparency Accessing inherited variables Textbox Lines Limit Newbie needs help with datagrid Read value of all columns from datatable together CreateObject error Need to start word 2003 and show existing rtf doc in VB.Net Accessing inherited variables |
|||||||||||||||||||||||