10

我正在尝试发出一个实例化 System.Lazy 的方法,并在该行以“无效令牌”的 PEVerify 错误失败newobj instance void class [mscorlib]System.Lazy`1<class Example.ExpensiveType>::.ctor(class [mscorlib]System.Func`1<class Example.ExpensiveType>)

用 ILDasm 看其他地方,我看到一个正确的调用看起来像这样:

newobj     instance void class [mscorlib]System.Lazy`1<class Example.IHeater>::.ctor(class [mscorlib]System.Func`1<!0>)

不幸的是,我不知道如何使用 Mono.Cecil API 重现它。有人可以帮助仿制药吗?

到目前为止,这是我所拥有的:

var get = new MethodDefinition(
            "Get",
            MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Virtual,
            ModuleDefinition.TypeSystem.Object);

var funcType = new GenericInstanceType(ImportedTypes.FuncOfT);
funcType.GenericArguments.Add(lazyElementType);

var funcTypeCtor = new MethodReference(".ctor", ModuleDefinition.TypeSystem.Void, funcType);
funcTypeCtor.Parameters.Add(new ParameterDefinition(ModuleDefinition.TypeSystem.Object));
funcTypeCtor.Parameters.Add(new ParameterDefinition(ModuleDefinition.TypeSystem.IntPtr));
funcTypeCtor.HasThis = true;
funcTypeCtor = ModuleDefinition.Import(funcTypeCtor);

var lazyTypeCtor = new MethodReference(".ctor", ModuleDefinition.TypeSystem.Void, lazyType);
var parameterDefinition = new ParameterDefinition(funcType);
lazyTypeCtor.Parameters.Add(parameterDefinition);
lazyTypeCtor.HasThis = true;
lazyTypeCtor = ModuleDefinition.Import(lazyTypeCtor);

il = get.Body.GetILProcessor();
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Ldftn, getTypedValue);
il.Emit(OpCodes.Newobj, funcTypeCtor);
il.Emit(OpCodes.Newobj, lazyTypeCtor);  // This leads to the invalid token
il.Emit(OpCodes.Ret);
lazyBinding.Methods.Add(get);

任何帮助将不胜感激 - 我很难过!

4

1 回答 1

11

我发现答案隐藏在一个多年前的邮件列表档案中(感谢 Gábor Kozár!)。我没有正确创建/导入泛型类型及其方法。正确加载Lazy<T>Func<T>类型的代码如下:

var genericArgument = lazyElementType;
var funcType = ModuleDefinition.ImportReference(typeof(Func<>)).MakeGenericInstanceType(genericArgument);
var funcCtor =
    ModuleDefinition.ImportReference(funcType.Resolve()
                                    .Methods.First(m => m.IsConstructor && m.Parameters.Count == 2))
                    .MakeHostInstanceGeneric(genericArgument);

var lazyType = ModuleDefinition.ImportReference(typeof(Lazy<>)).MakeGenericInstanceType(genericArgument);
var lazyCtor =
    ModuleDefinition.ImportReference(lazyType.Resolve()
                                    .GetConstructors()
                                    .First(m => m.Parameters.Count == 1
                                             && m.Parameters[0].ParameterType.Name.StartsWith("Func")))
                    .MakeHostInstanceGeneric(genericArgument);

// Method body as above

上面的关键是扩展方法MakeHostInstanceGeneric,它被定义为

public static MethodReference MakeHostInstanceGeneric(
                                  this MethodReference self,
                                  params TypeReference[] args)
{
    var reference = new MethodReference(
        self.Name,
        self.ReturnType,
        self.DeclaringType.MakeGenericInstanceType(args))
    {
        HasThis = self.HasThis,
        ExplicitThis = self.ExplicitThis,
        CallingConvention = self.CallingConvention
    };

    foreach (var parameter in self.Parameters) {
        reference.Parameters.Add(new ParameterDefinition(parameter.ParameterType));
    }

    foreach (var genericParam in self.GenericParameters) {
        reference.GenericParameters.Add(new GenericParameter(genericParam.Name, reference));
    }

    return reference;
}
于 2013-05-08T05:41:36.060 回答