Basically what I need is to use the Type that i got using Type.GetType in a Generic Type,
is it possible, if yes how ?
I need something like this:
Type t = Type.GetType("mynamespce.a.b.c");
var x = GenericClass<t>();
Basically what I need is to use the Type that i got using Type.GetType in a Generic Type,
is it possible, if yes how ?
I need something like this:
Type t = Type.GetType("mynamespce.a.b.c");
var x = GenericClass<t>();
It can be done: http://msdn.microsoft.com/en-us/library/b8ytshk6.aspx (See section "Constructing an Instance of a Generic Type")
The following example creates a Dictionary<string,object>
:
Type d1 = typeof(Dictionary<,>);
Type[] typeArgs = {typeof(string), typeof(object)};
Type constructed = d1.MakeGenericType(typeArgs);
object o = Activator.CreateInstance(constructed);
Type t = Type.GetType("mynamespce.a.b.c");
Type gt = typeof(GenericClass<>).MakeGenericType(t);
var x = Activator.CreateInstance(gt);
Yes, it is, but you will have to use further reflection. And you get the resulting object as a System.Object.
object obj = Activator.CreateInstance(typeof(GenericClass<>).MakeGenericType(Type.GetType("mynamespce.a.b.c")));
You can use Type.MakeGenericType and Activator.CreateInstance to make an instance of the generic type, e.g.
Type t = Type.GetType("mynamespce.a.b.c");
Type g = typeof(GenericClass<>).MakeGenericType(t);
object x = Activator.CreateInstance(g);
But it won't be strongly typed as the type of generic class in your code, if that's what you're looking for. That isn't possible as C# doesn't allow you to work with open generic types.