反射.Emit和泛型类型

本文关键字:泛型类型 Emit 反射 | 更新日期: 2023-09-27 18:06:25

我正在使用Reflection.Emit,我想创建一个类型,它将相当于c#中定义的以下类型:

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

技巧是我需要使用BCL中的泛型类型,该类型使用我的自定义类型作为泛型参数。

我弄乱了下面的代码片段:

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);

调用GetConstructor失败,出现以下异常:

NotSupportedException: Specified method is not supported.

因此,基本上,它不允许我获得仅引用自定义类型的类型的构造函数,也不能在发出其方法体之前完成该类型。

真的不可能摆脱这种恶性循环吗?

反射.Emit和泛型类型

出于某种原因,您需要使用GetConstructor()的静态过载来完成此操作。在您的示例中,代码可能如下所示:

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