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@dlev: Not quite. I think the OP wants it in human-readable form.Robert Harvey– Robert Harvey2011-07-04 18:21:56 +00:00Commented 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...)dlev– dlev2011-07-04 18:23:38 +00:00Commented Jul 4, 2011 at 18:23
-
I want to show in a dialogBox the IL code, so I can't use ildasm.Michaël– Michaël2011-07-04 18:26:15 +00:00Commented Jul 4, 2011 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...
8 Comments
CSharpCodeProvider will allow you to compile C# source code into IL.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.