Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# type or namespace expected confusion

Im getting slightly confused by a C# error.

Type t = thing.GetType()

t is now a type. but if i attempt to do this:

new GenericThing<t>

I get a warning saying type or namespace expected. What am i missing?

like image 212
richzilla Avatar asked Aug 19 '11 10:08

richzilla


1 Answers

t is a Type object created at runtime. Generics expect a type name, resolved at compile time. To create a generic at runtime, you have to use MakeGenericType

For example:

Activator.CreateInstance(typeof(GenericThing<>).MakeGenericType(t));
like image 65
Dark Falcon Avatar answered Sep 25 '22 14:09

Dark Falcon