1 Star 0 Fork 0

nygula / ILSpy

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
DecompilerNuGetDemos.workbook 2.40 KB
一键复制 编辑 原始数据 按行查看 历史
---
uti: com.xamarin.workbook
id: de255e90-ba7b-4070-be45-65e544ae8219
title: DecompilerPackageDemos
platforms:
- DotNetCore
packages:
- id: ICSharpCode.Decompiler
version: 3.0.0.3447
---
Setup: load the references required to work with the decompiler
```csharp
#r "ICSharpCode.Decompiler"
#r "Mono.Cecil"
using Mono.Cecil;
using ICSharpCode.Decompiler;
using ICSharpCode.Decompiler.CSharp;
using ICSharpCode.Decompiler.TypeSystem;
// Version sanity check
Console.WriteLine(typeof(FullTypeName).Assembly.GetName());
```
You must have compiled **frontends.sln** first (run “dotnet build” in ICSharpCode.Decompiler.PowerShell folder)
```csharp
string workbookBasePath = System.IO.Directory.GetCurrentDirectory();
string fileName = System.IO.Path.Combine(workbookBasePath, "ICSharpCode.Decompiler.PowerShell", "bin", "Debug", "netstandard2.0", "ICSharpCode.Decompiler.dll");
var decompiler = new CSharpDecompiler(fileName, new DecompilerSettings());
```
Get the count of types in this assembly
```csharp
var types = decompiler.TypeSystem.Compilation.MainAssembly.GetAllTypeDefinitions();
Console.WriteLine(types.Count());
```
Decompile a known type (as a whole)
```csharp
// ICSharpCode.Decompiler.Util.Empty<T> -> translates to `n, where n is the # of generic parameters
var nameOfGenericType = new FullTypeName("ICSharpCode.Decompiler.Util.Empty`1");
Console.WriteLine(decompiler.DecompileTypeAsString(nameOfGenericType));
```
If you want to decompile one single member (sample: first property)
```csharp
var nameOfUniResolver = new FullTypeName("ICSharpCode.Decompiler.UniversalAssemblyResolver");
ITypeDefinition typeInfo = decompiler.TypeSystem.Compilation.FindType(nameOfUniResolver).GetDefinition();
IMemberDefinition cecilProperty = decompiler.TypeSystem.GetCecil(typeInfo.Properties.First()).Resolve();
Console.WriteLine(decompiler.DecompileAsString(cecilProperty));
```
If you need the Cecil ModuleDefinition
```csharp
ITypeDefinition type = decompiler.TypeSystem.Compilation.FindType(nameOfUniResolver).GetDefinition();
var module = decompiler.TypeSystem.GetCecil(type).Module
```
Get the child namespaces
```csharp
var icsdns = decompiler.TypeSystem.Compilation.RootNamespace;
foreach (var ns in icsdns.ChildNamespaces) Console.WriteLine(ns.FullName);
```
Get types in a single namespace
```csharp
// ICSharpCode.Decompiler.TypeSystem is the first namespace
var typesInNamespace = icsdns.ChildNamespaces.First().Types;
```
C#
1
https://gitee.com/nygula/ILSpy.git
git@gitee.com:nygula/ILSpy.git
nygula
ILSpy
ILSpy
master

搜索帮助