ilspy-decompile

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

.NET Assembly Decompilation with ILSpy

使用ILSpy进行.NET程序集反编译

Use this skill to understand how .NET code works internally by decompiling compiled assemblies.
使用此技能,你可以通过反编译已编译的程序集来了解.NET代码的内部工作原理。

Prerequisites

前置条件

  • .NET SDK installed
  • ILSpy command-line tool available via one of the following:
    • dnx ilspycmd
      (if available in your SDK or runtime)
    • dotnet tool install --global ilspycmd
Both forms are shown below. Use the one that works in your environment.
Note: ILSpyCmd options may vary slightly by version.
Always verify supported flags with
ilspycmd -h
.
  • 已安装.NET SDK
  • 可通过以下方式之一获取ILSpy命令行工具:
    • dnx ilspycmd
      (若你的SDK或运行时中提供此命令)
    • dotnet tool install --global ilspycmd
以下展示了两种使用形式,请选择适配你环境的方式。
注意:ILSpyCmd的选项可能会因版本略有不同。 请始终通过
ilspycmd -h
验证支持的参数。

Quick start

快速开始

bash
undefined
bash
undefined

Decompile an assembly to stdout

将程序集反编译后输出到标准输出

ilspycmd MyLibrary.dll
ilspycmd MyLibrary.dll

or

或者

dnx ilspycmd MyLibrary.dll
dnx ilspycmd MyLibrary.dll

Decompile to an output folder

将反编译结果输出到指定文件夹

ilspycmd -o output-folder MyLibrary.dll
undefined
ilspycmd -o output-folder MyLibrary.dll
undefined

Common .NET Assembly Locations

.NET程序集常见位置

NuGet packages

NuGet包

bash
~/.nuget/packages/<package-name>/<version>/lib/<tfm>/
bash
~/.nuget/packages/<package-name>/<version>/lib/<tfm>/

.NET runtime libraries

.NET运行时库

bash
dotnet --list-runtimes
bash
dotnet --list-runtimes

.NET SDK reference assemblies

.NET SDK引用程序集

bash
dotnet --list-sdks
Reference assemblies do not contain implementations.
bash
dotnet --list-sdks
引用程序集不包含实现代码。

Project build output

项目构建输出

bash
./bin/Debug/net8.0/<AssemblyName>.dll
./bin/Release/net8.0/publish/<AssemblyName>.dll
bash
./bin/Debug/net8.0/<AssemblyName>.dll
./bin/Release/net8.0/publish/<AssemblyName>.dll

Core workflow

核心工作流程

  1. Identify what you want to understand
  2. Locate the assembly
  3. List types
  4. Decompile the target
  1. 确定你想要了解的目标内容
  2. 定位程序集位置
  3. 列出类型
  4. 反编译目标内容

Commands

命令说明

Basic decompilation

基础反编译

bash
ilspycmd MyLibrary.dll
ilspycmd -o ./decompiled MyLibrary.dll
ilspycmd -p -o ./project MyLibrary.dll
bash
ilspycmd MyLibrary.dll
ilspycmd -o ./decompiled MyLibrary.dll
ilspycmd -p -o ./project MyLibrary.dll

Targeted decompilation

定向反编译

bash
ilspycmd -t Namespace.ClassName MyLibrary.dll
ilspycmd -lv CSharp12_0 MyLibrary.dll
bash
ilspycmd -t Namespace.ClassName MyLibrary.dll
ilspycmd -lv CSharp12_0 MyLibrary.dll

View IL code

查看IL代码

bash
ilspycmd -il MyLibrary.dll
bash
ilspycmd -il MyLibrary.dll

Notes on modern .NET builds

现代.NET构建相关说明

  • ReadyToRun images may reduce readability
  • Trimmed or AOT builds may omit code
  • Prefer non-trimmed builds
  • ReadyToRun镜像可能会降低代码可读性
  • 经过裁剪或AOT构建的程序集可能会省略部分代码
  • 优先选择未裁剪的构建版本

Legal note

法律说明

Decompiling assemblies may be subject to license restrictions.
反编译程序集可能会受到许可证限制。