Reflecting on Generics

Have you ever had to do something like this:

string typeName = "MyDataType";
Type type = Type.GetType( typeName );
object o = someGenericClass<typeof(type)>();

If you have, then you have also discovered that this does not compile. The good news is this capability is possible in C#, but its not obvious. This article is aimed at unmuddying the waters surrounding reflection and generic types. We will show how to accomplish this with a generic class (and, as an added bonus, the equivalent functionality for generic methods too).

Read more...

 

No Comments