Skip to content

Getting Started With ICSharpCode.Decompiler

JMV edited this page Jul 3, 2018 · 7 revisions
  1. Create a new project and add the ICSharpCode.Decompiler nuget to your project.
  2. Add the following usings to your code:
using ICSharpCode.Decompiler;
using ICSharpCode.Decompiler.CSharp;
using ICSharpCode.Decompiler.TypeSystem;
  1. Now you can open an assembly file for decompilation with default settings:
var decompiler = new CSharpDecompiler(fileName, new DecompilerSettings());
  1. You can either decompile a type or member to a SyntaxTree (for further processing) by using the Decompile*overloads or to a string using the Decompile*AsString overloads.
  • 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
Clone this wiki locally