从 MethodInfo 生成 DynamicMethod
我正在研究 Joel Pobar 的避开常见的性能陷阱来制作快速的应用程序 关于反射的文章,我正在查看一段未编译的特定代码(稍微修改以缩小特定错误的范围,因为他的示例有更多错误):
MethodInfo writeLine = typeof(Console).GetMethod("WriteLine");
RuntimeMethodHandle myMethodHandle = writeLine.MethodHandle;
DynamicMethod dm = new DynamicMethod(
"HelloWorld", // name of the method
typeof(void), // return type of the method
new Type[]{}, // argument types for the method
false); // skip JIT visibility checks
ILGenerator il = dm.GetILGenerator();
il.Emit(OpCodes.Ldstr, "Hello, world");
il.Emit(OpCodes.Call, myMethodHandle); // <-- 2 errors here
il.Emit(OpCodes.Ret);
错误是:
Program.cs(350,13): error CS1502: The best overloaded method match for 'System.Reflection.Emit.ILGenerator.Emit(System.Reflection.Emit.OpCode, byte)' has some invalid arguments
Program.cs(350,35): error CS1503: Argument '2': cannot convert from 'System.RuntimeMethodHandle' to 'byte'
ILGenerator
可以使用 MethodInfo
来 Emit
,但它似乎不支持 MethodHandle
...有人知道如何让这个示例工作吗?
I was going over a Joel Pobar's Dodge Common Performance Pitfalls to Craft Speedy Applications article on Reflection and I was looking at a particular piece of code that isn't compiling (slightly modified to narrow down to the specific error, because his example had more errors):
MethodInfo writeLine = typeof(Console).GetMethod("WriteLine");
RuntimeMethodHandle myMethodHandle = writeLine.MethodHandle;
DynamicMethod dm = new DynamicMethod(
"HelloWorld", // name of the method
typeof(void), // return type of the method
new Type[]{}, // argument types for the method
false); // skip JIT visibility checks
ILGenerator il = dm.GetILGenerator();
il.Emit(OpCodes.Ldstr, "Hello, world");
il.Emit(OpCodes.Call, myMethodHandle); // <-- 2 errors here
il.Emit(OpCodes.Ret);
The errors are:
Program.cs(350,13): error CS1502: The best overloaded method match for 'System.Reflection.Emit.ILGenerator.Emit(System.Reflection.Emit.OpCode, byte)' has some invalid arguments
Program.cs(350,35): error CS1503: Argument '2': cannot convert from 'System.RuntimeMethodHandle' to 'byte'
The ILGenerator
can Emit
with a MethodInfo
, but it doesn't seem to support MethodHandle
... does anybody know how to get this sample to work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
像这样吗?
更改:
ILGenerator
想要的)EmitCall
(另一个也可能有效,但我知道这种方式有效)Like so?
Changes:
GetMethod
to find the(string)
overload (otherwise it is an ambiguous match)MethodInfo
, not the handle (since that is whatILGenerator
wants)EmitCall
(the other might have worked too, but I know this way works)通过 Nuget.org 上的这个库:
ClassWrapper
它为内部使用动态生成方法的类型创建包装器。因此,没有使用反射(仅进入 ClassWrapperDescriptor.Load 方法来生成动态表达式)
给定,例如名为 SampleClass 的类,
非常感谢有关此库的任何反馈!
Through this library on Nuget.org:
ClassWrapper
It creates wrappers for types that internally uses dynamically generated methods. So no reflection used (only into the ClassWrapperDescriptor.Load method to generate the dynamic expressions)
Given, e.g. e class named SampleClass
Any feedback on this library is really appreciated!