Michaël
Michaël

Reputation: 6734

Convert C# code to IL code

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.

Upvotes: 17

Views: 18222

Answers (6)

Regis Portalez
Regis Portalez

Reputation: 4860

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.

Upvotes: 1

Rahul
Rahul

Reputation: 77876

OR,

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

Upvotes: 1

Jethro
Jethro

Reputation: 5916

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

Upvotes: 1

BrokenGlass
BrokenGlass

Reputation: 160892

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

Upvotes: 6

Teoman Soygul
Teoman Soygul

Reputation: 25742

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.

Upvotes: 6

Jon Skeet
Jon Skeet

Reputation: 1500515

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...

Upvotes: 32

Related Questions