使用Emit从int调用ToString时,操作可能会破坏运行时错误

Rao*_*ron 0 c# reflection reflection.emit

这段代码有效

var toString = typeof(string).GetMethod("ToString", new Type[] { });

var dm = new DynamicMethod("MyToString", typeof(string), new Type[] { typeof(string) });

var il = dm.GetILGenerator();

il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Callvirt, toString);
il.Emit(OpCodes.Ret);

Delegate d = dm.CreateDelegate(typeof(Func<string, string>));

var r = d.DynamicInvoke("10");
Run Code Online (Sandbox Code Playgroud)

此代码抛出异常(System.Security.VerificationException:Operation可能会破坏运行时的稳定性.)

var toString = typeof(int).GetMethod("ToString", new Type[] { });

var dm = new DynamicMethod("MyToString", typeof(string), new Type[] { typeof(int) });

var il = dm.GetILGenerator();

il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Callvirt, toString);
il.Emit(OpCodes.Ret);

Delegate d = dm.CreateDelegate(typeof(Func<int, string>));

var r = d.DynamicInvoke(10);
Run Code Online (Sandbox Code Playgroud)

为什么?

小智 9

有了Ldarg_0你加载参数0的值有关的值类型调用实例方法T,隐含this参数没有类型T,它有一个类型ref T,所以你需要不加载的价值,但争论0.参考Ldarga指令会为你做那件事.这是当前的问题.

一个不太严重的问题,虽然对我来说未被发现并且我不能100%确定是否严格要求,但是你应该是Call关于值类型的实例方法的指令,或者在某些特定情况下,指令Constrained前的前缀Callvirt.

一般来说,不要猜测你需要什么CIL指令.在C#中编写您想要的代码一次,编译它,并反汇编结果.这会告诉您可以轻松查看的确切说明.