问题
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