Meitaim.Decompiler.Tool.any
1.1.0
Prefix Reserved
dotnet add package Meitaim.Decompiler.Tool.any --version 1.1.0
NuGet\Install-Package Meitaim.Decompiler.Tool.any -Version 1.1.0
<PackageReference Include="Meitaim.Decompiler.Tool.any" Version="1.1.0" />
<PackageVersion Include="Meitaim.Decompiler.Tool.any" Version="1.1.0" />
<PackageReference Include="Meitaim.Decompiler.Tool.any" />
paket add Meitaim.Decompiler.Tool.any --version 1.1.0
#r "nuget: Meitaim.Decompiler.Tool.any, 1.1.0"
#:package Meitaim.Decompiler.Tool.any@1.1.0
#addin nuget:?package=Meitaim.Decompiler.Tool.any&version=1.1.0
#tool nuget:?package=Meitaim.Decompiler.Tool.any&version=1.1.0
Meitaim.Decompiler.Tool
Decompiles .NET assemblies to C# from the command line.
dotnet tool install --global Meitaim.Decompiler.Tool
This installs the dotnet decompile command. dotnet decompile --help lists every option. For the library, see Meitaim.Decompiler.
Decompile an assembly
Without --output, the C# goes to standard output:
dotnet decompile Some.dll
dotnet decompile --output Some.cs Some.dll
Decompile many assemblies
Name a directory to read every .dll, .exe and .winmd in it, or a pattern where ** matches any depth of directories. Quote a pattern so that the shell does not expand it. Each assembly becomes one file, so --output names a directory:
dotnet decompile --output decompiled bin/Release
dotnet decompile --output decompiled "bin/**/*.dll"
Files that a pattern finds in subdirectories keep their subdirectory under --output.
Decompile only some types
List the top level types, then name each one that you want with --type:
dotnet decompile --list-types Some.dll
dotnet decompile --type Some.Namespace.Program --output Program.cs Some.dll
dotnet decompile --type Some.Namespace.Program --type Some.Namespace.Startup --output types Some.dll
Each type becomes one file, so two or more types need a directory.
Find referenced assemblies
The decompiler reads referenced assemblies to spell names, operators and overrides correctly. The tool looks for them in the directory of each assembly, then in the directories of --reference in their order, then in the .NET runtime that runs the tool:
dotnet decompile --reference Game/Managed --reference libs --output Plugin.cs Plugin.dll
References are found by name, and the first file with that name wins, whatever its version. For a .NET Framework, Mono or Unity assembly, add the directory that holds its references, because the tool runs on a newer .NET.
Compare the public surface of two versions
--no-bodies writes signatures without method bodies:
dotnet decompile --no-bodies --output old.cs old/Some.dll
dotnet decompile --no-bodies --output new.cs new/Some.dll
diff old.cs new.cs
Change the output
dotnet decompile --indent tab --newline crlf --block-namespaces --no-keywords --output Some.cs Some.dll
--indent takes tab or a number of spaces from 0 to 16. --block-namespaces wraps each namespace in braces. --no-keywords writes System.Int32 where int would do.
--lang-version preview writes C# that needs <LangVersion>preview</LangVersion>. The default, latest, writes C# for the newest released C#.
Recompile the output
--project writes a .csproj beside the C# of each assembly:
dotnet decompile --project --output decompiled --reference Game/Managed Game/Managed/Assembly-CSharp.dll
dotnet build decompiled/Assembly-CSharp.csproj
With --project, the C# also writes every using directive that it needs, so the project does not depend on implicit global usings.
The project references, by its file, each assembly that the decompiler found outside the .NET runtime. When two references declare a type of the same name, the C# names the type through an extern alias, and the project gives that reference the alias. An assembly of the .NET runtime never gets an alias. A reference that the code reaches only through another reference, and that declares a type a direct reference also declares, gets only its alias, so that its types do not clash with the direct reference.
The project targets the framework that the assembly names when its standard library came from the .NET runtime. An assembly that names no framework gets one from its standard library: .NET Framework 3.5 for mscorlib 2.0, .NET Framework 4.8.1 for a later mscorlib, the .NET Standard of its netstandard, or the .NET release that holds its standard library.
- For .NET, the target framework supplies the runtime's assemblies. An assembly from ASP.NET Core or another shared framework becomes a
FrameworkReference. - For .NET Framework, the project names each assembly that the code references directly and that the target framework has, such as
System.Windows.Forms. The reference assemblies of the target framework supply it. - For .NET Standard, the target framework supplies the assemblies it has, such as
System.Runtime, so the project leaves them out. - For .NET Framework and .NET Standard, an assembly that the target framework lacks and that only the .NET runtime holds cannot serve the build. The tool says so on standard error, so that you can add
--referencewith the directory that holds the right one.
Otherwise, the project references the standard library file too, such as the mscorlib.dll of a Unity game, and turns the implicit framework references off.
The tool also says on standard error which references it found in no directory and the target framework lacks, since the project leaves them out.
The project does not carry over the manifest resources of the assembly, such as .resources files, so code that reads them builds but finds nothing at run time.
Each project keeps its build files in obj/<project name>/ and bin/<project name>/, and picks up no files on its own, so many projects can share one output directory. The project also ignores Directory.Build.props, Directory.Build.targets and Directory.Packages.props in the directories above it.
--project needs --output and method bodies, and works only for whole assemblies. An --output that does not end in .cs names a directory.
Make repeated runs faster
--cache keeps the C# of each whole assembly in a directory. The next run copies it from there while the assembly, its references, the options and the tool version stay the same:
dotnet decompile --cache .cache --output decompiled "bin/**/*.dll"
The cache works only for runs that write whole assemblies to files, without --type, --list-types or --diagnostics.
--threads sets how many types of one assembly the tool writes at once. The default is one for each processor. With --threads 1, the tool also writes one assembly at a time.
Check what the decompiler could not resolve
--diagnostics writes to standard error the places in each assembly where the C# may not match the IL. Each kind gives its total, then one line for each method, with its parameter types, that says how many it has there, for example:
$ dotnet decompile --diagnostics --output Some.cs Some.dll
Some.dll:
2 guessed operator spellings
op_Addition in Shapes.Coin.Shapes.IAdder.op_Addition(IAdder, IAdder)
op_Subtraction in Shapes.Coin.Shapes.ISubtracter.op_Subtraction(ISubtracter, ISubtracter)
3 pinned locals left unfixed
2 in Shapes.Grid<T>.Copy(int, Span<byte>)
1 in Shapes.Grid<T>.Fill(byte)
1 assembly attributes left unspelled
assembly: TypeForwardedTo(typeof(Other.Moved))
A guessed operator line gives the operator name first. An assembly attribute line gives its target and the attribute.
The library README explains each kind.
Exit codes
The tool exits with 0 when it wrote everything, 1 when the command line is wrong, and 2 when something else failed, such as an assembly that it could not read or a type that it could not find.
Learn more about Target Frameworks and .NET Standard.
This package has no dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Highlight: `dotnet decompile --project` writes a .csproj next to the C#, so the output builds with the right references, extern aliases and target framework.
Features:
- `--project` writes a .csproj for each assembly. Types forwarded into another assembly that clash with direct references now get extern aliases too.
- `Diagnostics.All` and `Diagnostics.TakeAll()` list each diagnostic with the method it occurs in and how many times. The 1.0.0 counters still work but are obsolete.
- The library and the tool each have their own README.
Fixes:
- Extern aliases now parse inside interpolated strings, and aliases named like a keyword or `global` compile.
- Anonymous delegates are written as source writes them, so they no longer fail with CS1908.
- Lambdas keep their parameter defaults and `params` arrays.
- Parameter and return attributes, and attributes stored as metadata flags (`MethodImpl`, `Serializable`, `NonSerialized`, `ComImport`, `SpecialName`, `Optional`, `AssemblyVersion`), are written.
- `const decimal` fields and decimal parameter defaults come back, so they're no longer `static readonly` or missing.
- `DefaultMember` stays on types without an indexer, and indexers not named `Item` keep their name through `IndexerName`.
Performance:
- The native tool uses a fresh PGO profile, recorded for each platform.