How to 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:

public class Test<T> {}
public class Test<T1, T2> {}

When I call ModuleBuilder.DefineType (string) with a second type declaration, I get an exception because there is already another type in the module with the same name (I already defined the type parameter for the first type). Any ideas?

+5
source share
1 answer

You should avoid conflict just like C # and VB.Net do. When generating a generic type name, add a character and the number of common parameters. For example, for the above

the following type names are actually generated:
class Test`1 // Test<T>
class Test`2 // Test<T1,T2>

BCL . IL #, , , .

+3

All Articles