[英]How can I define multiple types with the same name and different type parameters using Reflection Emit?
How can I generate types like these using the System.Reflection.Emit libraries: 如何使用System.Reflection.Emit库生成类似这样的类型:
public class Test<T> {}
public class Test<T1, T2> {}
When I call ModuleBuilder.DefineType(string) with the second type declaration, I get an exception because there is already another type in the module with the same name (I've already defined the type parameter on the first type). 当我使用第二个类型声明调用ModuleBuilder.DefineType(string)时,我得到一个异常,因为模块中已经存在另一个具有相同名称的类型(我已经在第一个类型上定义了类型参数)。 Any ideas?
有任何想法吗?
You should avoid the conflict in the same way that C# and VB.Net do. 您应该像C#和VB.Net一样避免冲突。 When emiting a generic type name append a ` symbol and the number of generic parameters.
当发出泛型类型名称时,附加一个`符号和泛型参数的数量。 For example the following type names actually get generated for the above
例如,实际上为上面生成了以下类型名称
class Test`1 // Test<T>
class Test`2 // Test<T1,T2>
You can view this name mangling in the BCL with reflector. 您可以使用反射器在BCL中查看此名称。 Set the language to IL instead of C# and it will show the actual names of type as emitted in metadata instead of the prettified language name.
将语言设置为IL而不是C#,它将显示在元数据中发出的类型的实际名称,而不是美化语言名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.