Reflection.Emit - access topmost-but-one item from stack

╄→尐↘猪︶ㄣ 提交于 2020-02-20 07:22:21

问题


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?


回答1:


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.




回答2:


+1 for kvbs answer, see: http://www.codeproject.com/KB/msil/msilenhancement.aspx




回答3:


Inline with what kvb said, you could try a small function to do some reordering. Not sure if it would be any faster.




回答4:


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.



来源:https://stackoverflow.com/questions/718003/reflection-emit-access-topmost-but-one-item-from-stack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!