Convert C# code to IL code

為{幸葍}努か 提交于 2019-12-04 08:23:12

问题


How I can get IL code of C# code ? Can I do this with a extern library, or exists internal functions ?

EDIT : I want to show IL code in my application with a MessageBox.


回答1:


Programmatically? You can use reflection to get a MethodInfo, and then call MethodBase.GetMethodBody to get the body. From the MethodBody, you can call GetILAsByteArray amongst other things.

Of course, if you just want to examine it yourself, there's Reflector, dotPeek, ildasm (part of the .NET SDK) and no doubt other tools...




回答2:


Use IL Disassembler like this:

c:\il>csc Class.cs    
c:\il>ildasm /output=Class.il Class.exe

you'll both have the IL & the exe.




回答3:


Just open a Visual Studio command prompt and type ildasm - with ildasm you can open up any assembly and show the generated IL.




回答4:


Take a look at LinqPad, it has options to see IL.




回答5:


OR,

Start -> Programs -> Microsoft .NET Framework SDK (Version) -> Tools -> MSIL Disassembler



回答6:


It's quite an old question, but I'd like to add some accuracies.

GetMethodBody() will give you at best a byte array, which you need to parse manually (which can be painful since some opcodes have two bytes) to get a human readable text.

I just want to mention ILSpy as an alternate solution.

It's open source, meaning you can debug it and integrate with it in your solution.



来源:https://stackoverflow.com/questions/6574858/convert-c-sharp-code-to-il-code

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