简体   繁体   English

如何使用Reflection Emit定义具有相同名称和不同类型参数的多个类型?

[英]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.

相关问题 我可以定义两个具有相同名称但参数不同的代表吗? - Can I define 2 delegates with the same name but different parameters? 如何使用 System.Reflection.Emit 从 T 创建派生类型 - How can I create a derived type from T, using System.Reflection.Emit 如何使用IL Emit定义彼此引用的两种类型 - How can I define two types referencing each other using IL Emit 如何:在c#中使用反射发射定义自引用类型属性 - How to: Define a Self Referenced Type Property with Reflection Emit in c# 如果参数名称和字段名称使用反射不同,如何检索参数的值? - How do I retrieve the values for parameters if the parameter name and field name are different using reflection? 使用Reflection.Emit实例化具有通用参数的泛型类型 - Using Reflection.Emit to instantiate Generic Type with Generic Parameters 有没有办法,使用反射,我可以将一个类型传递给一个方法,然后让该方法以与 nameof 相同的方式找到该类型的名称 - Is there a way, using Reflection that I can pass a type into a method and then have that method find the name of that type in the same way as nameof Reflection.Emit如何分配不兼容的类型? - How can Reflection.Emit assign incompatible types? 我可以使用System.Reflection.Emit将动态方法添加到现有类型吗? - Can I add a dynamic method to an existing type using System.Reflection.Emit? 如何使用反射确定属性类型? - How can I determine property types using reflection?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM