"Joël" <Jo*@discussions.microsoft.com> wrote in message
news:DC**********************************@microsof t.com...
Hi!
I want to do something like that :
class Test<T>{
}
string myVar = "System.String";
Type myType = Type.GetType( myVar );
Test<myType> delta = new Test<myType>();
Is it possible to do something like that ?
Thanks for your help
As written, no. You can create generic types via reflection using an
arbitrary type, but I'm not sure what it would buy you. What are you trying
to achieve with this code?
basic generic type construction code(untested, but should work):
string myVar = "System.String";
Type myType = Type.GetType( myVar );
Type testType = typeof(Test<>);
Type genericType = testType.MakeGenericType(new Type[] { myType });
object o = Activator.CreateInstance(genericType);
but you will only get as far as object or antoher base. You cannot treat the
reference as the given generic type due to compile time type checking.