Pass An Instantiated System.Type as a Type Parameter for a Generic Class

The title is kind of obscure. What I want to know is if this is possible:

string typeName = <read type name from somwhere>;
Type myType = Type.GetType(typeName);

MyGenericClass<myType> myGenericClass = new MyGenericClass<myType>();

Obviously, MyGenericClass is described as:

public class MyGenericClass<T>

Right now, the compiler complains that ‘The type or namespace ‘myType’ could not be found.” There has got to be a way to do this.

6 Answers
6

Leave a Comment