I have a scenario on which i got Class name as a string
I just want to create a generic List
of that class. I tried the following
Type _type = Type.GetType(className);
List<_type> list = new List<_type>();
But iam geting error
. I can't gave that _type as list argument. If anybody knows please help me
Unfortunately, this is not possible as generics need to be known at compile time. You are trying to redefine this by making them known at runtime. The reason they need to be known at compile time is so the compiler can verify that all variables passed to it are valid. AFAIK, doing what you want to go would require a complete rewrite of generics.
The main reason behind compile time checking is that
_type
can change at runtime and then there would be overhead in adding to theList<T>
as the JIT compiler would need to do type checking.What @MarkusJarderot said is the closest you can get as interfaces are used instead of generics.
Closest you can get:
Reflection takes place during runtime, as the type of an object could be changed to anything by a running program.
Type parameters (generics), on the other hand, are only used by the compiler, to check type safety.
So it's not possible to use reflection specify a type parameter, because the compiler will not know anything about the result of reflection.
If you are using .Net 4, then using
dynamic
may help. In this case you will be able to use methods on the instance that are not accessible through non-generic interfaces (such asIList
).For example, using
dynamic
allows you to invoke theAddRanges
method of the list, which you couldn't do through a cast toIList
:However, this method is not as nearly as type-safe as casting to a non-generic interface, because it cannot catch errors at compile time.