I need to convert the IL to c# code. I have an assembly and I am reading this assembly. I get MethodBody from MethodInfo and the methodbody has a method GetILAsByteArraY() which returns a byte array now I want to convert this IL to C# code. Please help me out in solving this.
-
1What do you need this for? If you describe your situation in somewhat more detail, maybe we can suggest more than .NET Reflector. – Anton Tykhyy Mar 15 '09 at 16:14
-
1@Anton We'd probably be better off not understanding this hackery. – BC. Mar 15 '09 at 16:15
-
Did anyone suggest Reflector yet? – Michael Burr Mar 15 '09 at 16:20
-
Obviously, he wants to do this from code. – Robert Jeppesen Sep 07 '09 at 13:26
-
ILSPy has the code in github https://github.com/icsharpcode/ILSpy and you can take what you like to use from it – Walter Verhoeven May 20 '20 at 12:14
12 Answers
Don't reinvent the wheel; use Lutz Roeder's .NET Reflector.

- 236,483
- 35
- 423
- 525
-
24I think it is clearly obvious that he is trying to create another wheel like that of .NET Reflector and he wants to know how it's made. Lutz shouldn't necessarily be the only man in the world who knows how to make it. – Muhammad Hasan Khan Mar 15 '09 at 18:39
-
@HasanKhan given OP's choice of answer he is pretty happy with Lutz being the only man :) – nawfal Jun 30 '16 at 22:58
The Lutz Roeder reflector was purchased and is no longer available for free download.
There is now a free (once again) tool called ILSpy. I've found it to be very good, and it will not only generate C# code from IL but will also create a .csproj
project if decompiling an entire DLL assembly.

- 32,612
- 11
- 57
- 93

- 15,232
- 11
- 67
- 85
Re-Inventing the wheel surely a better way to learn! you will have to create your own class that would convert iL to C#.This requires the knowledge of Intermediate language.
I assume you have already converted the Byte Array to IL instructions just like we see in ildasm.exe,Using Reflection.Emit.OpCodes,OpCode.OperandType,BitConvertor.ReadByte(),BitConvertor.ReadInt32 etc.

- 91
- 1
- 2
-
https://stackoverflow.com/questions/69423090/convert-il-to-c-sharp-programmatically This is my question, How can we get C# from IL ? – Oct 03 '21 at 08:02
If you want to do it yourself, you could start from the old Cecil.Decompiler and enhance it, see the source code and an old post of Jb. Evain announcing the project (Link down).

- 17,397
- 4
- 57
- 75
-
Fixed the first one, can't do anything about the second one as I don't know why he put his blog offline even if he still reference it on twitter. – Julien Roncaglia Mar 28 '13 at 07:58
Let's see if I manage to get it in first: .NET Reflector! This really should be a FAQ...

- 17,166
- 1
- 38
- 51
Can't you do this with Reflector?
You can load an assembly and view it as VB.net/C#/IL...

- 1,442
- 18
- 32
For the search engine people, Reflector ain't free anymore. But a newer and better and not to mention FREE tool is being offered from telerik called JustDecompile. I just downloaded this and its so awesome and its just beta. Apparently telerik promises free forever.

- 56,733
- 95
- 279
- 406
Maybe you should try Dis# if you don't wanna re-invent the wheel... It's almost the best tool in converting the code NetDASM has a tool that lets you convert C# to IL. maybe you should take a look at that.

- 1,554
- 1
- 28
- 36
You should just use .NET Reflector if you have the assembly. Also try the file gen addon.

- 24,298
- 12
- 47
- 62
Reflector does this. Perhaps you could use Reflector against Reflector and learn how it handles the problem.
You might also say why you want to do this - there may be a better solution to your overall problem.

- 160,644
- 26
- 247
- 397
-
3Reflectors internals were obfuscated using non-printables (at least lutz did that). While yes you could reflect on itself a better question would is it really worth that... – JoshBerke Mar 15 '09 at 16:14
-
Yes, that's what I suspected. It's why I asked him why he wants to do it. – John Saunders Mar 15 '09 at 16:53
Let me be the first to suggest you grab a copy of Red Gate's Reflector.
-
1@ck Well, you answer a question and after hitting Submit fifty other people have already answered. O well... – Jul 19 '10 at 18:05