2011-04-06 53 views
0

我试图解决这个问题:反思,仿制药和多组件

Type.GetType("Class1'[[Class2]]")

其中Class1Class2在不同的组件。

我可以解析组件,并找到Class1类型以及该Class2类型,但我如何才能在Class1<Class2>类型?

+0

你为什么得到带有字符串的类型,他们是那些程序集中的内部类? – 2011-04-07 04:08:01

回答

1

我想,这应该是这样的:

Type.GetType("Class1`1[Class2]"); 

注:我从'到'改变了APOSTROPH并加入通用参数的个数。

如果这还不够,尝试指定的类,包括命名空间和装配:

Type.GetType("Namespace1.Class1`1[[Namespace2.Class2, Assembly2]], Assembly1"); 
+0

MakeGenericType()解决方案更加优雅 – 2012-06-19 22:13:50

2

如果你能找到的类型,所有你需要的是:

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>)

+0

完美,这就是我一直在寻找的!谢谢! – Thomas 2011-04-08 17:26:58

+0

您可以将其标记为问题的解决方案,这样其他用户无需询问即可找到答案。 – 2011-04-11 04:44:59

+0

这个答案不够好,被标记为正确吗? – Ric 2013-11-14 16:05:52