C# 反射、泛型和多个程序集
我正试图解决这个问题:C# 反射、泛型和多个程序集,c#,generics,reflection,C#,Generics,Reflection,我正试图解决这个问题: Type.GetType(“Class1'[[Class2]]) 其中Class1和Class2位于不同的组件中 我可以解析程序集并找到Class1类型以及Class2类型,但是如何找到Class1类型?我认为,它应该是这样的: Type.GetType("Class1`1[Class2]"); 注意:我将撇号从“改为”,并添加了泛型参数的数量 如果这还不够,请尝试指定包括命名空间和程序集在内的类: Type.GetType("Namespace1.Class1`1[[
Type.GetType(“Class1'[[Class2]])
其中Class1
和Class2
位于不同的组件中
我可以解析程序集并找到
Class1
类型以及Class2
类型,但是如何找到Class1
类型?我认为,它应该是这样的:
Type.GetType("Class1`1[Class2]");
注意:我将撇号从“改为”,并添加了泛型参数的数量
如果这还不够,请尝试指定包括命名空间和程序集在内的类:
Type.GetType("Namespace1.Class1`1[[Namespace2.Class2, Assembly2]], Assembly1");
我想,应该是这样的:
Type.GetType("Class1`1[Class2]");
注意:我将撇号从“改为”,并添加了泛型参数的数量
如果这还不够,请尝试指定包括命名空间和程序集在内的类:
Type.GetType("Namespace1.Class1`1[[Namespace2.Class2, Assembly2]], Assembly1");
如果可以找到类型,则只需:
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)
如果您可以找到所有需要的类型:
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)
为什么要获得带有字符串的类型,这些程序集中是否有内部类?为什么要获得带有字符串的类型,这些程序集中是否有内部类?您可以将其标记为问题的解决方案,这样其他用户无需提问即可找到答案。此答案是否不够好,无法标记为正确?您可以将其标记为问题的解决方案,这样其他用户无需提问即可找到答案。此答案是否不够好,无法标记为正确?MakeGenericType()解决方案更加优雅MakeGenericType()解决方案更加优雅