Skip to content

Getting Started With ICSharpCode.Decompiler

Siegfried Pammer edited this page Dec 7, 2022 · 7 revisions

Note: Please refer to the Jupyter notebook for always up-to-date samples.

  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.FindType(name).GetDefinition();
var tokenOfFirstMethod = typeInfo.Methods.First().MetadataToken;
Console.WriteLine(decompiler.DecompileAsString(tokenOfFirstMethod));
  • If you need access to low-level metadata tables:
ITypeDefinition type = decompiler.TypeSystem.FindType(nameOfUniResolver).GetDefinition();
var module = type.ParentModule.PEFile;
  • Get the child namespaces:
var icsdns = decompiler.TypeSystem.RootNamespace;
foreach (var ns in icsdns.ChildNamespaces) Console.WriteLine(ns.FullName);
  • Get types in a single namespace:
// ICSharpCode.Decompiler.TypeSystem is the first namespace
var typesInNamespace = icsdns.ChildNamespaces.First().Types;