【问题标题】:C# Emit create dynamic property ToString methodC# Emit 创建动态属性 ToString 方法
【发布时间】:2019-03-21 11:19:50
【问题描述】:

我有一个类似下面代码的类:

public class MyClass
{
    public int MyProperty1 { get; set; }
}

我希望创建一个动态方法调用tostring

public static string MyProperty1ToString(MyClass o){
    return o.MyProperty1.ToString();
}

IL

MyProperty1ToString:
IL_0000:  nop         
IL_0001:  ldarg.0     
IL_0002:  callvirt    UserQuery+MyClass.get_MyProperty1
IL_0007:  stloc.0     
IL_0008:  ldloca.s    00 
IL_000A:  call        System.Int32.ToString
IL_000F:  stloc.1     
IL_0010:  br.s        IL_0012
IL_0012:  ldloc.1     
IL_0013:  ret   

所以我尝试使用 Emit 创建方法,但得到错误

InvalidProgramException : 公共语言运行时检测到无效程序

public class Program
{
    public static void Main()
    {
        var obj = new MyClass() { MyProperty1 = 123 };
        var prop = obj.GetType().GetProperty("MyProperty1");

        var func = GetByPropertyCallToStringFunction<MyClass>(prop);
        var data = func(obj); //InvalidProgramException : common language runtime detected an invalid program
    }

    public static Func<T, string> GetByPropertyCallToStringFunction<T>(PropertyInfo prop)
    {
        var type = prop.DeclaringType;
        var propGetMethod = prop.GetMethod;
        var propType = prop.PropertyType;

        DynamicMethod dynamicMethod = new DynamicMethod($"{prop.Name}_method", typeof(string), new Type[] { type }, type.Module);

        var toStringMethod = propType.GetMethods(BindingFlags.Public | BindingFlags.Instance).Where(p => p.Name == "ToString").First();

        ILGenerator il = dynamicMethod.GetILGenerator();
        il.Emit(OpCodes.Ldarg_0);
        il.Emit(OpCodes.Callvirt, propGetMethod);
        il.Emit(OpCodes.Stloc);
        il.Emit(OpCodes.Ldloca_S, 00);
        il.Emit(OpCodes.Call, toStringMethod);
        il.Emit(OpCodes.Stloc_1);
        il.Emit(OpCodes.Br_S, "IL_0012");
        il.Emit(OpCodes.Ldloc_1);
        il.Emit(OpCodes.Ret);

        var invoke = (Func<T, string>)dynamicMethod.CreateDelegate(typeof(Func<T, string>));
        return invoke;
    }
}

问题

如何获得发出运行时编译器详细信息的错误消息?
我不知道什么时候只有InvalidProgramException


测试链接:C# Emit to create dynamic Property ToString method | C# Online Compiler | .NET Fiddle

【问题讨论】:

  • Br_S的参数应该是il.MarkLabel定义的标签。我假设你得到了错误,因为它不知道IL_0012 的意思;即使是这样,反汇编的(Linqpad?)IL 代码示例和您的 Emit 代码之间的偏移无论如何都不会匹配,因为您省略了 Nop 指令。
  • 我已经在这里解决了这个问题,但仅此一项并不能解决问题。
  • 是的,这是 linqpad,谢谢@ckuri

标签: c# emit


【解决方案1】:

知道了。 除了必须明确声明分支标签(ckuri 已经正确发现)这一事实之外,您还必须在引用它们之前声明本地变量 - 包括它们的类型。

代码如下:

var propType = prop.PropertyType;
var propGetMethod = prop.GetMethod;

ILGenerator il = dynamicMethod.GetILGenerator();
LocalBuilder local0 = il.DeclareLocal(typeof(propType));
LocalBuilder local1 = il.DeclareLocal(typeof(string));
Label label0 = il.DefineLabel();

il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Callvirt, propGetMethod);
il.Emit(OpCodes.Stloc, local0);
il.Emit(OpCodes.Ldloca_S, local0);
il.Emit(OpCodes.Call, toStringMethod);
il.Emit(OpCodes.Stloc, local1);
il.Emit(OpCodes.Br_S, label0);
il.MarkLabel(label0);
il.Emit(OpCodes.Ldloc, local1);
il.Emit(OpCodes.Ret);

return (Func<T, string>)dynamicMethod.CreateDelegate(typeof(Func<T, string>));

然而,这个版本并没有进行太多优化。看来您是从为调试模式编译的代码中获取的。

下面的版本也是这样,但效率更高,主要是不需要标签,只需要一个本地:

var propType = prop.PropertyType;
var propGetMethod = prop.GetMethod;

ILGenerator iLGenerator = dynamicMethod.GetILGenerator();
LocalBuilder local0 = iLGenerator.DeclareLocal(propType);

iLGenerator.Emit(OpCodes.Ldarg_0);
iLGenerator.Emit(OpCodes.Call, propGetMethod);
iLGenerator.Emit(OpCodes.Stloc, local0);
iLGenerator.Emit(OpCodes.Ldloca_S, local0);
iLGenerator.Emit(OpCodes.Call, toStringMethod);
iLGenerator.Emit(OpCodes.Ret);
return (Func<T, string>)dynamicMethod.CreateDelegate(typeof(Func<T, string>));

【讨论】:

    猜你喜欢
    • 2014-09-30
    • 1970-01-01
    • 2010-10-31
    • 2010-10-26
    • 2020-01-08
    • 1970-01-01
    • 1970-01-01
    • 2022-06-24
    相关资源
    最近更新 更多