-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Getting Started With ICSharpCode.Decompiler
JMV edited this page Jul 3, 2018
·
7 revisions
- Create a new project and add the
ICSharpCode.Decompilernuget to your project. - Add the following usings to your code:
using ICSharpCode.Decompiler;
using ICSharpCode.Decompiler.CSharp;
using ICSharpCode.Decompiler.TypeSystem;- Now you can open an assembly file for decompilation with default settings:
var decompiler = new CSharpDecompiler(fileName, new DecompilerSettings());- You can either decompile a type or member to a
SyntaxTree(for further processing) by using theDecompile*overloads or to a string using theDecompile*AsStringoverloads.
- If you want to decompile a specific type by name, you can use
DecompileType*(FullTypeName):
The FullTypeName(string) supports reflection syntax.
var decompiler = new CSharpDecompiler("Demo.ConsoleApp.exe", new DecompilerSettings());
var name = new FullTypeName("Demo.ConsoleApp.Test+NestedClassTest");
Console.WriteLine(decompiler.DecompileTypeAsString(name));- If you want to decompile one single member:
var decompiler = new CSharpDecompiler("Demo.ConsoleApp.exe", new DecompilerSettings());
var name = new FullTypeName("Demo.ConsoleApp.Test+NestedClassTest");
ITypeDefinition typeInfo = decompiler.TypeSystem.Compilation.FindType(name).GetDefinition();
IMemberDefinition cecilProperty = decompiler.TypeSystem.GetCecil(typeInfo.Properties.First()).Resolve();
Console.WriteLine(decompiler.DecompileAsString(cecilProperty));- If you need the Cecil ModuleDefinition
var decompiler = new CSharpDecompiler("Demo.ConsoleApp.exe", new DecompilerSettings());
var name = new FullTypeName("Demo.ConsoleApp.Test+NestedClassTest");
ITypeDefinition type = decompiler.TypeSystem.Compilation.FindType(name).GetDefinition();
var module = decompiler.TypeSystem.GetCecil(type).Module