Reflection.Emit - 访问堆栈中最顶层的一项
.NET 中是否有一种方法,使用 Reflection.Emit
来访问堆栈中最顶层的一项? 因此,如果 A 位于最上面,B 接下来 - 我想处理 B,然后处理 A。将 B 复制到 A 上方就可以了(因为当我到达第二个 B 时,我可以简单地“弹出”第二个 B) )。
目前,我正在声明本地:
LocalBuilder loc = il.DeclareLocal(typeof(Foo));
il.Emit(OpCodes.Stloc, loc); // store and pop topmost stack item
// work with (pop) previous stack item
il.Emit(OpCodes.Ldloc, loc); // push old topmost stack item
是否有一条不需要显式本地的路由?
Is there a way in .NET, using Reflection.Emit
, to access the topmost-but-one item from the stack? So if A is topmost, and B next - I want to process B then A. It would be fine to duplicate B above A (since I can simply "pop" the second B when I get to it).
Currently, I am declaring a local:
LocalBuilder loc = il.DeclareLocal(typeof(Foo));
il.Emit(OpCodes.Stloc, loc); // store and pop topmost stack item
// work with (pop) previous stack item
il.Emit(OpCodes.Ldloc, loc); // push old topmost stack item
Is there a route that doesn't need the explicit local?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不这么认为。 在 IL 中,没有任何像交换这样的指令可以让你做你想做的事。 为什么您认为使用当地人是令人反感的? 如果 JIT 编译器足够好,那么与在 IL 中使用假设的交换操作相比,这不会导致任何更慢的机器代码。
I don't think so. In IL there aren't any instructions like swap which would allow you to do what you want. Why do you see using a local as objectionable? If the JIT compiler is good enough this won't result in any slower machine code than using a hypothetical swap operation in IL.
+1 kvbs 答案,请参阅: http://www.codeproject.com/KB/ msil/msilenhancement.aspx
+1 for kvbs answer, see: http://www.codeproject.com/KB/msil/msilenhancement.aspx
与 kvb 所说的一致,您可以尝试一个小函数来进行一些重新排序。 不确定是否会更快。
Inline with what kvb said, you could try a small function to do some reordering. Not sure if it would be any faster.
我遇到了同样的问题。 我想要生成一个相当大的方法,并且经常想要“交换”以存储计算值。 我对 ildasm 中出现大量本地人感到不满意,并注意到 BeginScope/EndScope 没有任何帮助。 我最终为我的方法的上下文创建了一个本地“交换”,并在每个交换操作中重用它。 使得生成的IL更加干净; 不确定它是否对性能有任何有意义的影响。
I encountered this same problem. I wanted to generate a rather large method and I often wanted to 'swap' in order to store a calculated value. I was unhappy with the large volume of locals showing up in ildasm and noticed that BeginScope/EndScope wasn't any help. I wound up creating a local 'swap' for the context of my method and reusing it for every swap operation. It makes the generated IL cleaner; not sure if it has any meaningful impact on performance.