.NET 4.5 MethodBuilder.SetMethodBody [英] .NET 4.5 MethodBuilder.SetMethodBody

查看:25
本文介绍了.NET 4.5 MethodBuilder.SetMethodBody的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在最新版本的 .NET 框架 4.5 版中,MethodBuilder 类有一个名为 SetMethodBody 的方法,我认为这正是我正在寻找的替代使用 ILGenerator(它很烦人并且以奇怪的方式受到限制).可以在此处找到该文档.a>,虽然 .NET 4.5 还没有发布,但它没有完整的文档.我可以提供除两个参数之外的所有参数,但其余的我需要帮助.

In the newest version of the .NET framework, version 4.5, the MethodBuilder class has a method called SetMethodBody that I believe is exactly what I'm looking at as an alternative to using ILGenerator (which is annoying and limited in odd ways). The documentation can be found here, although since .NET 4.5 is not out yet, it is not fully documented. I can supply all but two of the arguments, but the rest I will need help with.

第一个我不明白的是byte[] localSignature,第三个参数.MSDN 声明它是包含序列化局部变量结构的字节数组.如果该方法没有局部变量,则指定 null."问题是,仅此而已,我无法找出序列化局部变量签名"的格式.我曾尝试查看 ECMA-335 规范,但我发现的只是如何在未组装的 CIL 中指定局部变量.如果有人能帮我解决这个问题,我将不胜感激.

The first that I don't understand is byte[] localSignature, the third argument. MSDN states that it is "An array of bytes that contain the serialized local variable structure. Specify null if the method has no local variables." The trouble is, that's all it says, and I can't find out the format of a "serialized local variable signature." I have tried looking in the ECMA-335 spec, but all I have found is how to specify the local variables in unassembled CIL. If anybody could help me figure this out, it would be much appreciated.

另外,最后一个参数是 IEnumerable;tokenFixups,它是表示 il 中偏移量的值的集合,每个值指定可以修改的标记的开头.如果该方法没有必须修改的标记,则指定 null.".我怀疑我不需要使用这些,但无论如何我想知道它们是什么.

Also, the last argument is IEnumerable<int> tokenFixups, which is "A collection of values that represent offsets in il, each of which specifies the beginning of a token that may be modified. Specify null if the method has no tokens that have to be modified.". I suspect that I won't need to use these, but I'd like to know what they are anyway.

谢谢,布兰登

推荐答案

我的问题的真正答案是作为评论而不是答案发布的,所以如果其他人有这个问题......这里是发布的答案:

The real answer to my question was posted as a comment, instead of an answer, so in case anybody else ever has this question... here's the posted answer:

您将需要 SignatureHelper 类.修正仅适用于将本机代码转换为 IL 的编译器,如 C++/CLI.– Hans Passant 3 月 10 日 13:02

You'll need the SignatureHelper class. Fixups are only for compilers that translate native code to IL, like C++/CLI. – Hans Passant Mar 10 at 13:02

所以...为了获取本地签名的字节数组,您可以执行以下代码:

So... in order to get the byte array for the local signatures, you can execute this code:

var sig = SignatureHelper.GetLocalVarSigHelper(this.module);
sig.AddArgument(typeof(int)); //Local #0 is of type int
...
sig.AddArgument(typeof(string)); //Local #n is of type string
var sigArray = sig.GetSignature();

为了在 MethodBuilder 上设置方法体,您调用

And in order to set the method body on the MethodBuilder, you call

MethodBuilder.SetMethodBody(il, maxStack, sigArray, handlers, fixups);

...其中 il 是带有有效 IL 指令的 byte[](请参阅 this page),maxStack 是一个整数,表示方法在堆栈上保留的点数,handlers 是一个 System.Reflection.Emit.ExceptionHandler[] 和 fixups 是一个可以忽略的 int[] 数组(有一个例外,见下文.)

...where il is a byte[] with valid IL instructions (see this page), maxStack is an integer with the number of spots to reserve on the stack for the method, handlers is an System.Reflection.Emit.ExceptionHandler[], and fixups is a int[] array that can be ignored (with one exception, see below.)

我在 Hans Passant 的评论中不同意的一件事是,修正不仅适用于将本机代码转换为 IL 的编译器.我在处理此问题时发现,如果您尝试发出对 MethodBuilder 方法的调用,它会发出错误的指令.查看 .NET 反射器中的 ILGenerator,我发现它们每次发出方法调用时都会发出一个修正.为每个方法调用添加一个修正确实解决了这个问题.可能还有其他地方您需要发出修正才能使其正常工作,但我并没有深入研究.

One thing that I disagree with in Hans Passant's comment is that fixups are not only for compilers that translate native code into IL. I discovered when working on this that if you try to emit a call to a MethodBuilder method, it emits the wrong instruction. Looking at ILGenerator in .NET reflector, I found out that they emit a fixup each time they emit a method call. Adding a fixup for each method call indeed fixed this problem. There may be other places where you need to emit a fixup for it to work correctly, but I haven't looked into it much.

这篇关于.NET 4.5 MethodBuilder.SetMethodBody的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆