How can I generate types like these using the System.Reflection.Emit libraries:
如何使用System.Reflection生成这样的类型。发出库:
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). Any ideas?
当我使用第二个类型声明调用modulebuild . definetype (string)时,我得到一个异常,因为模块中已经有另一个具有相同名称的类型(我已经在第一个类型上定义了类型参数)。什么好主意吗?
1 个解决方案
#1
3
You should avoid the conflict in the same way that C# and VB.Net do. 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
您应该以与c#和VB相同的方式避免冲突。净做。当emiting一个泛型类型名称时,附加一个“符号”和泛型参数的数量。例如,下面的类型名称实际上是为上面生成的
class Test`1 // Test<T>
class Test`2 // Test<T1,T2>
You can view this name mangling in the BCL with reflector. 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.
您可以使用reflector在BCL中查看这个名称。将语言设置为IL而不是c#,它将显示元数据中发出的类型的实际名称,而不是经过修饰的语言名称。
#1
3
You should avoid the conflict in the same way that C# and VB.Net do. 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
您应该以与c#和VB相同的方式避免冲突。净做。当emiting一个泛型类型名称时,附加一个“符号”和泛型参数的数量。例如,下面的类型名称实际上是为上面生成的
class Test`1 // Test<T>
class Test`2 // Test<T1,T2>
You can view this name mangling in the BCL with reflector. 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.
您可以使用reflector在BCL中查看这个名称。将语言设置为IL而不是c#,它将显示元数据中发出的类型的实际名称,而不是经过修饰的语言名称。