How do I get an IL bytearray from a DynamicMethod?

前端 未结 4 1728
感情败类
感情败类 2020-12-17 21:14

As a bit of a novelty, I\'m trying to see how different the IL from light weight code generated at runtime looks vs code generated by the VS compiler, as I noticed that VS c

4条回答
  •  清歌不尽
    2020-12-17 21:43

    Based off Hans Passant's work I was able to dig a little deeper there appears to be a method that you should call, called BakeByteArray so the following works::

    var dynMethod = fiOwner.GetValue(compiled.Method) as DynamicMethod;
    var ilgen =dynamicMethod.GetILGenerator();
    byte[] il = ilgen.GetType().GetMethod("BakeByteArray", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(ilgen, null) as byte[];
    

    This certainly helps, but I still have no way to resolve VariableInfo's just yet which is something that would help in my work.

提交回复
热议问题