Omu*_*Omu 0 .net c# reflection
基本上
我有
string s = "Foo"
Run Code Online (Sandbox Code Playgroud)
我需要获得
Type t = IRepo<Foo>
Run Code Online (Sandbox Code Playgroud)
但是从字符串
这样的东西,使用Type.MakeGenericType:
Type arg = Type.GetType(s);
Type definition = typeof(IRepo<>);
Type concrete = definition.MakeGenericType(arg);
Run Code Online (Sandbox Code Playgroud)
请注意,Type.GetType(string)有几点需要注意:
mscorlib调用程序集以外的程序集中获取类型,则必须包含程序集名称