-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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.
- Create a new project and add the
ICSharpCode.Decompiler
nuget 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*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;