Embedd_0913
Embedd_0913

Reputation: 16565

Converting IL to C# code

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.

Upvotes: 18

Views: 28741

Answers (12)

DuckMaestro
DuckMaestro

Reputation: 15905

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.

Upvotes: 16

Julien Roncaglia
Julien Roncaglia

Reputation: 17837

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

Upvotes: 3

Shawn Mclean
Shawn Mclean

Reputation: 57479

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.

Upvotes: 1

Javed S
Javed S

Reputation: 91

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.

Upvotes: 9

Achilles
Achilles

Reputation: 1734

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.

Upvotes: 0

user1228
user1228

Reputation:

Let me be the first to suggest you grab a copy of Red Gate's Reflector.

Upvotes: 0

John Saunders
John Saunders

Reputation: 161821

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.

Upvotes: 0

JoshBerke
JoshBerke

Reputation: 67108

There is no tool in the BCL that will do this; however, there is a great tool called Reflector, which you can write your own add-ins for. So you could probally use this to get the data you want.

Upvotes: 0

BC.
BC.

Reputation: 24958

You should just use .NET Reflector if you have the assembly. Also try the file gen addon.

Upvotes: 0

Ward Werbrouck
Ward Werbrouck

Reputation: 1462

Can't you do this with Reflector?

You can load an assembly and view it as VB.net/C#/IL...

Upvotes: 1

Pontus Gagge
Pontus Gagge

Reputation: 17278

Let's see if I manage to get it in first: .NET Reflector! This really should be a FAQ...

Upvotes: 1

jason
jason

Reputation: 241771

Don't reinvent the wheel; use Lutz Roeder's .NET Reflector.

Upvotes: 23

Related Questions