basically
I have
string s = "Foo"
I need to obtain
Type t = IRepo<Foo>
but from string
basically
I have
string s = "Foo"
I need to obtain
Type t = IRepo<Foo>
but from string
Type t = typeof (IRepo<>).MakeGenericType(Type.GetType(s));
Something like this, using Type.MakeGenericType
:
Type arg = Type.GetType(s);
Type definition = typeof(IRepo<>);
Type concrete = definition.MakeGenericType(arg);
Note that Type.GetType(string)
comes with a few caveats:
mscorlib
or the calling assembly, you have to include the assembly nameYou can do the following:
var someTypeName = "Foo";
var someType = Type.GetType("Namespace.To." + someTypeName);
typeof(IRepo<>).MakeGenericType(someType);
You first need to get the Type
of Foo
, and then you can pass that into Type.MakeGenericType.