简体   繁体   English

反射,泛型和多个程序集

[英]Reflection, generics and multiple assemblies

I'm trying to solve this: 我正在尝试解决这个问题:

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

where Class1 and Class2 are in different assemblies. 其中Class1Class2在不同的程序集中。

I can parse the assemblies and find the Class1 type as well as the Class2 type, but how do I get to the Class1<Class2> type? 我可以解析程序集并找到Class1类型和Class2类型,但是如何获取Class1<Class2>类型?

if you can find the types all you need to is: 如果您可以找到所有类型,则只需:

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 will be like having typeof(Class1<Class2>) genericType就像具有typeof(Class1<Class2>)

I think, it should look like this: 我认为应该看起来像这样:

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

Note: I changed the apostroph from ' to ` and added the number of generic arguments. 注意:我将撇号从'更改为`,并添加了通用参数的数量。

If this is not enough, try specifying the classes including namespace and assembly: 如果这还不够,请尝试指定包括名称空间和程序集的类:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM