keyongtech


  keyongtech > dotnet.languages.* > dotnet.languages.vb > 06/2006

 #1  
06-05-06, 07: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.
 #2  
06-05-06, 08:24 PM
Herfried K. Wagner [MVP]
"cmay" <cmay> 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) _
)
///
 #3  
06-05-06, 09:00 PM
cmay
Thanks HKW!


Herfried K. Wagner [MVP] wrote:
[..]
 #4  
06-06-06, 06: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

"Herfried K. Wagner [MVP]" <hirf-spam-me-here> schreef in bericht
news:1208
[..]
 #5  
06-06-06, 11:36 AM
Herfried K. Wagner [MVP]
"Cor Ligthert [MVP]" <notmyfirstname> 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.
Similar Threads
Generics Type Intersection

[..] Daniel Pitts did an essay on Java generics type intersection. It is no longer at this URL. It is available anywhere else?

Using Generics vs Passing System.Type as argument?

I have am currently working on a set of c# utility methods that all require a System.Type object as an input argument, either soley or along with other arguments. In call...

Generics and Type casting.

Hi all, I have interface declared like public IBaseInterface { } then a generic collection like

generics and type checking

All, I'm giving generics a shot, and I'm getting this warning: Type safety: The cast from Object to ArrayList<RequestBean> is actually checking against the erased type What...

Type Casting With Generics

I've been reading a discussion thread at [..] regarding typecasting generic collections to classical collections and vice-a-versa I faced a similar problem and solved it...


All times are GMT. The time now is 06:01 PM. | Privacy Policy