我试图解决这个问题:反思,仿制药和多组件
Type.GetType("Class1'[[Class2]]")
其中Class1
和Class2
在不同的组件。
我可以解析组件,并找到Class1
类型以及该Class2
类型,但我如何才能在Class1<Class2>
类型?
我试图解决这个问题:反思,仿制药和多组件
Type.GetType("Class1'[[Class2]]")
其中Class1
和Class2
在不同的组件。
我可以解析组件,并找到Class1
类型以及该Class2
类型,但我如何才能在Class1<Class2>
类型?
我想,这应该是这样的:
Type.GetType("Class1`1[Class2]");
注:我从'到'改变了APOSTROPH并加入通用参数的个数。
如果这还不够,尝试指定的类,包括命名空间和装配:
Type.GetType("Namespace1.Class1`1[[Namespace2.Class2, Assembly2]], Assembly1");
MakeGenericType()解决方案更加优雅 – 2012-06-19 22:13:50
如果你能找到的类型,所有你需要的是:
Type class1Type = assembly1.GetType("Class1"); //or however you are able to get this type
Type class2Type = assembly2.GetType("Class2"); //or however you are able to get this type
Type genericType = class1Type.MakeGenericType(class2Type);
genericType会像其typeof(Class1<Class2>)
你为什么得到带有字符串的类型,他们是那些程序集中的内部类? – 2011-04-07 04:08:01