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.

- 6,676
- 3
- 36
- 55
-
1@dlev: Not quite. I think the OP wants it in human-readable form. – Robert Harvey Jul 04 '11 at 18:21
-
1@Robert: Perhaps, though the question is sufficiently vague that I felt a (somewhat) snarky comment was warranted. Honestly, without more context, I'm not 100% sure what is being asked (i.e. do they want to see IL in human-readable form from compiled code, do they want to pass in some code to a function which returns an array of IL instructions...) – dlev Jul 04 '11 at 18:23
-
I want to show in a dialogBox the IL code, so I can't use ildasm. – Michaël Jul 04 '11 at 18:26
6 Answers
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...

- 1,421,763
- 867
- 9,128
- 9,194
-
-
I want a c# method, so your first sentence seems the good answer. Thanks Jon! – Michaël Jul 04 '11 at 18:33
-
@malinois: I assume you're after *already compiled* C# code as IL, by the way? `CSharpCodeProvider` will allow you to compile C# *source* code into IL. – Jon Skeet Jul 04 '11 at 18:38
-
@Jon, I see. I still think "Jon Skeet" is a team of developers, even though you were on thisDevelopersLife. You are the face of the group. ;) – Jethro Jul 04 '11 at 18:40
-
@Jethro: If someone were picking a face for a group of developers, I'd hope they could come up with a better one than mine :) – Jon Skeet Jul 04 '11 at 18:40
-
@Jon, hahaha, we shall battle on these dev grounds again someday. IE (Getting late in RSA and I need some sleep.) :) – Jethro Jul 04 '11 at 18:43
-
@Jon, I use CSharpCodeProvider to create dlls with .cs file, but not to get IL code. It's possible to do that ? I wnat to make a functionnality like .net reflector, to show IL code of a dll using reflexion. – Michaël Jul 04 '11 at 18:48
-
@Jon, Have you an answer for this question ? http://stackoverflow.com/questions/6570457 – Michaël Jul 04 '11 at 18:53
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.

- 25,584
- 6
- 69
- 80
Just open a Visual Studio command prompt and type ildasm - with ildasm you can open up any assembly and show the generated IL.

- 158,293
- 28
- 286
- 335
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.

- 28,498
- 28
- 50
- 59

- 4,675
- 1
- 29
- 41
OR,
Start -> Programs -> Microsoft .NET Framework SDK (Version) -> Tools -> MSIL Disassembler

- 76,197
- 13
- 71
- 125