Skip to content

Commit

Permalink
Update DecompilerNuGetDemos.workbook to work with version 4.0 pre-alp…
Browse files Browse the repository at this point in the history
…ha nuget.
  • Loading branch information
siegfriedpammer committed Jul 17, 2018
1 parent abb8dac commit 3a7a8c4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
31 changes: 17 additions & 14 deletions DecompilerNuGetDemos.workbook
Expand Up @@ -6,18 +6,19 @@ platforms:
- DotNetCore
packages:
- id: ICSharpCode.Decompiler
version: 3.1.0.3652
version: 4.0.0.4193-alpha1-debug
---

Setup: load the references required to work with the decompiler

```csharp
#r "ICSharpCode.Decompiler"
#r "Mono.Cecil"
#r "System.Reflection.Metadata"

using Mono.Cecil;
using System.Reflection.Metadata;
using ICSharpCode.Decompiler;
using ICSharpCode.Decompiler.CSharp;
using ICSharpCode.Decompiler.Metadata;
using ICSharpCode.Decompiler.TypeSystem;

// Version sanity check
Expand All @@ -29,44 +30,46 @@ You must have compiled **frontends.sln** first (run “dotnet build” in ICShar
```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());
var module = new PEFile(fileName);
var assemblyResolver = new UniversalAssemblyResolver(fileName, true, module.Reader.DetectTargetFrameworkId());
var decompiler = new CSharpDecompiler(module, assemblyResolver, new DecompilerSettings());
```

Get the count of types in this assembly
Get the count of types in this module

```csharp
var types = decompiler.TypeSystem.Compilation.MainAssembly.GetAllTypeDefinitions();
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");
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));
ITypeDefinition typeInfo = decompiler.TypeSystem.FindType(nameOfUniResolver).GetDefinition();
var tokenOfFirstProperty = typeInfo.Properties.First().MetadataToken;
Console.WriteLine(decompiler.DecompileAsString(tokenOfFirstProperty));
```

If you need the Cecil ModuleDefinition
If you need access to low-level metadata tables

```csharp
ITypeDefinition type = decompiler.TypeSystem.Compilation.FindType(nameOfUniResolver).GetDefinition();
var module = decompiler.TypeSystem.GetCecil(type).Module
ITypeDefinition type = decompiler.TypeSystem.FindType(nameOfUniResolver).GetDefinition();
var module = type.ParentModule.PEFile;
```

Get the child namespaces

```csharp
var icsdns = decompiler.TypeSystem.Compilation.RootNamespace;
var icsdns = decompiler.TypeSystem.RootNamespace;
foreach (var ns in icsdns.ChildNamespaces) Console.WriteLine(ns.FullName);
```

Expand Down
Expand Up @@ -4,7 +4,7 @@
<id>ICSharpCode.Decompiler</id>
<version>$INSERTVERSION$$INSERTVERSIONNAMEPOSTFIX$$INSERTBUILDCONFIG$</version>
<title>ILSpy Decompiler Engine</title>
<authors>Daniel Grunwald, David Srbecky, Ed Harvey, Siegfried Pammer</authors>
<authors>ILSpy Contributors</authors>
<owners>Daniel Grunwald, SharpDevelop</owners>
<licenseUrl>https://opensource.org/licenses/mit-license.php</licenseUrl>
<projectUrl>https://github.com/icsharpcode/ILSpy/</projectUrl>
Expand Down

0 comments on commit 3a7a8c4

Please sign in to comment.