17

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.

3
  • 1
    @dlev: Not quite. I think the OP wants it in human-readable form. Commented Jul 4, 2011 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...) Commented Jul 4, 2011 at 18:23
  • I want to show in a dialogBox the IL code, so I can't use ildasm. Commented Jul 4, 2011 at 18:26

6 Answers 6

32

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

Sign up to request clarification or add additional context in comments.

8 Comments

What would you do with that? Build your own compiler?
I want a c# method, so your first sentence seems the good answer. Thanks Jon!
@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, 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: If someone were picking a face for a group of developers, I'd hope they could come up with a better one than mine :)
|
6

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.

Comments

6

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

Comments

1

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

Comments

1

OR,

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

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.