Reflection.Emit and generic types

二次信任 提交于 2019-12-10 15:21:10

问题


I am using Reflection.Emit and I want to create a type that would be the equivalent of the following type defined in C#:

class A
{
    public Tuple<A, int> GetValue(int x)
    {
         return new Tuple<A, int>(this, x);
    }
}

The trick is that I need to use a generic type from BCL that uses my custom type as a generic argument.

I'm messing with the following snippet:

var asmName = new AssemblyName("Test");
var access = AssemblyBuilderAccess.Run;
var asm = AppDomain.CurrentDomain.DefineDynamicAssembly(asmName, access);
var module = asm.DefineDynamicModule("Test");

var aType = module.DefineType("A");
var tupleType = typeof(Tuple<,>).MakeGenericType(aType, typeof(int));

var attrs = MethodAttributes.Public;
var method = aType.DefineMethod("GetValue", attrs, tupleType, new [] { typeof(int) });
var gen = method.GetILGenerator();

gen.Emit(OpCodes.Ldarg_0);
gen.Emit(OpCodes.Ldarg_1);

// here is the fail:
var ctor = tupleType.GetConstructor(new [] { typeof(int), aType } );
gen.Emit(OpCodes.Newobj, ctor);

The call to GetConstructor fails with the following exception:

NotSupportedException: Specified method is not supported.

So, basically, it won't let me get the constructor of a type that merely references my custom type, and neither can I finalize the type before emitting the body of its method.

Can it really be impossible to get out of this vicious circle?


回答1:


For some reason, you need to use the static overload of GetConstructor() to do this. In your case, the code could look like this:

var ctor = TypeBuilder.GetConstructor(
    tupleType, typeof(Tuple<,>).GetConstructors().Single());


来源:https://stackoverflow.com/questions/15669387/reflection-emit-and-generic-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!