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.
You can’t do this without reflection. However, you can do it with reflection. Here’s a complete example:
Note: if your generic class accepts multiple types, you must include the commas when you omit the type names, for example: