How do I decompile a class in C#?

412 Views Asked by At

How do I get a class' source code inside a C# program?

class Foo {
    string void Bar(){
        return "baz";
    }
}

Then in another file:

GetSourceCode(Foo) // => class Foo { string void Bar() { return "baz": } }

I was told ILSpy could do it based on this question: Can Roslyn generate source code from an object instance?, but there is absolutely no new usage examples at all.

The code in that linked question is out of date. AssemblyDefinition, Assembly, CSharpLanguage and DecompilationOptions are all unavailable in the current context, despite using ICSharpCode.Decompiler, ICSharpCode.Decompiler.CSharp and ICSharpCode.Decompiler.TypeSystem;

https://github.com/icsharpcode/ILSpy

1

There are 1 best solutions below

5
On

I have used dotPeek which is a free tool from JetBrains. Very good and simple to use. You will need the DLL's or the executable that you want to decompile

Helped me save an entire of folder that I rolled back by accident once