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

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

问题描述

在.NET框架中,4.5版的最新版本,该MethodBuilder类有一个方法 SetMethodBody ,我认为是我在寻找什么,在作为除了使用的ILGenerator(这是恼人的和有限的以奇怪的方式)。该文件可以发现<一href="http://msdn.microsoft.com/en-us/library/system.reflection.emit.methodbuilder.setmethodbody%28v=vs.110%29.aspx">here,虽然自.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.

首先,我不明白的是字节[] localSignature ,第三个参数。 MSDN说,它是包含序列化的局部变量结构中的字节数组。指定NULL,如果方法没有局部变量。麻烦的是,这一切是说,我不能找到一个格式为序列化的局部变量的签名。我试图寻找在ECMA-335规范,但我所发现的是如何在未组装CIL指定本地变量。如果有人可以帮助我弄清楚了这一点,这将是更AP preciated。

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&LT; INT&GT; tokenFixups ,这是是重新present中的偏移IL值,其中每一个规定,可以修改令牌的开头。指定空的集合,如果方法没有标记有进行修改。。我怀疑我会不会需要使用这些,但我想知道它们是什么呢。

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类。修复程序只对编译器的翻译原生code到IL,如C ++ / CLI。 - 汉斯帕桑特 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

所以......为了得到字节数组的地方签名,可以执行该code:

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指令(见的这个页面),MAXSTACK是斑点预留栈的方法对数的整数,处理程序是 System.Reflection.Emit.ExceptionHandler [] 和调整信息是 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.)

这是我在汉斯帕桑特的评论不同意一件事是调整信息不仅对编译器的翻译原生code到IL。我对这个工作,如果你试图发出呼叫到 MethodBuilder 方法,它发出了错误的指令时发现的。看的ILGenerator在.NET反射,我发现它们发出的每次发出的方法调用一次修正。添加一个链接地址信息为每个方法调用确实解决了这个问题。可能还有其他地方,你需要发出修正为它才能正常工作,但我还没有看进去了。

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天全站免登陆