代码拉取完成,页面将自动刷新
---
uti: com.xamarin.workbook
id: de255e90-ba7b-4070-be45-65e544ae8219
title: DecompilerPackageDemos
platforms:
- DotNetCore
packages:
- id: ICSharpCode.Decompiler
version: 6.2.0.6118-preview2
---
Setup: load the references required to work with the decompiler
```csharp
#r "ICSharpCode.Decompiler"
#r "System.Reflection.Metadata"
using System.Reflection.Metadata;
using ICSharpCode.Decompiler;
using ICSharpCode.Decompiler.CSharp;
using ICSharpCode.Decompiler.Metadata;
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", "Release", "netstandard2.0", "ICSharpCode.Decompiler.dll");
var decompiler = new CSharpDecompiler(fileName, new DecompilerSettings());
```
Get the count of types in this module
```csharp
var types = decompiler.TypeSystem.MainModule.TypeDefinitions;
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 method)
```csharp
var nameOfUniResolver = new FullTypeName("ICSharpCode.Decompiler.Metadata.UniversalAssemblyResolver");
ITypeDefinition typeInfo = decompiler.TypeSystem.FindType(nameOfUniResolver).GetDefinition();
var tokenOfFirstMethod = typeInfo.Methods.First().MetadataToken;
Console.WriteLine(decompiler.DecompileAsString(tokenOfFirstMethod));
```
If you need access to low-level metadata tables
```csharp
ITypeDefinition type = decompiler.TypeSystem.FindType(nameOfUniResolver).GetDefinition();
var module = type.ParentModule.PEFile;
```
Get the child namespaces
```csharp
var icsdns = decompiler.TypeSystem.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;
```
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。