第12章 集成与自动化
第12章:集成与自动化
12.1 Visual Studio 集成
12.1.1 安装扩展
扩展 → 管理扩展 → 搜索 ".NET Reactor"
下载并安装
重启 Visual Studio
12.1.2 项目配置
<!-- 在 .csproj 中添加 -->
<PropertyGroup>
<ReactorEnabled>true</ReactorEnabled>
<ReactorProject>protection.nrproj</ReactorProject>
</PropertyGroup>
<Target Name="ProtectAssembly" AfterTargets="Build" Condition="'$(Configuration)' == 'Release'">
<Exec Command=""$(ReactorPath)" -file "$(TargetPath)" -project "$(ReactorProject)"" />
</Target>
12.1.3 构建后事件
Post-build event:
"C:\Program Files (x86)\Eziriz\.NET Reactor\dotNET_Reactor.Console.exe" ^
-file "$(TargetPath)" ^
-targetfile "$(TargetDir)Protected\$(TargetFileName)" ^
-project "$(ProjectDir)protection.nrproj"
12.2 MSBuild 集成
12.2.1 自定义任务
<!-- ReactorTask.targets -->
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask TaskName="ReactorTask" AssemblyFile="Reactor.MSBuild.dll" />
<Target Name="ProtectWithReactor" AfterTargets="Build">
<ReactorTask
InputFile="$(TargetPath)"
OutputFile="$(TargetDir)Protected\$(TargetFileName)"
ProjectFile="$(ProjectDir)protection.nrproj"
Obfuscation="true"
NecroBit="true"
/>
</Target>
</Project>
12.2.2 条件执行
<Target Name="ProtectAssembly"
AfterTargets="Build"
Condition="'$(Configuration)' == 'Release' AND '$(EnableReactor)' == 'true'">
<!-- 保护逻辑 -->
</Target>
12.3 NuGet 包保护
12.3.1 打包前保护
<Target Name="ProtectBeforePack" BeforeTargets="Pack">
<Exec Command="dotNET_Reactor.Console.exe -file $(TargetPath) ..." />
</Target>
12.3.2 自动化流程
1. 构建项目
↓
2. 保护程序集
↓
3. 创建 NuGet 包
↓
4. 发布到 NuGet
12.4 Docker 集成
12.4.1 Dockerfile
# Dockerfile
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
WORKDIR /src
# 复制项目文件
COPY ["MyApp/MyApp.csproj", "MyApp/"]
RUN dotnet restore "MyApp/MyApp.csproj"
# 复制源代码
COPY . .
WORKDIR "/src/MyApp"
RUN dotnet build -c Release -o /app/build
# 安装 .NET Reactor
COPY ["dotnet-reactor/", "/opt/dotnet-reactor/"]
# 保护程序集
RUN mono /opt/dotnet-reactor/dotNET_Reactor.Console.exe \
-file /app/build/MyApp.dll \
-targetfile /app/protected/MyApp.dll \
-project protection.nrproj
# 最终镜像
FROM mcr.microsoft.com/dotnet/aspnet:8.0
WORKDIR /app
COPY --from=build /app/protected .
ENTRYPOINT ["dotnet", "MyApp.dll"]
12.5 自动化测试
12.5.1 单元测试
[TestClass]
public class ProtectedAssemblyTests
{
[TestMethod]
public void TestProtectedAssembly_LoadsSuccessfully()
{
// 加载保护后的程序集
var assembly = Assembly.LoadFile("protected/MyApp.dll");
Assert.IsNotNull(assembly);
}
[TestMethod]
public void TestProtectedAssembly_FunctionalityWorks()
{
// 测试保护后的功能
var type = assembly.GetType("MyApp.Calculator");
var instance = Activator.CreateInstance(type);
var method = type.GetMethod("Add");
var result = method.Invoke(instance, new object[] { 2, 3 });
Assert.AreEqual(5, result);
}
}
12.5.2 集成测试
[TestClass]
public class ProtectionIntegrationTests
{
[TestMethod]
public void TestCompleteProtectionPipeline()
{
// 1. 构建项目
BuildProject();
// 2. 保护程序集
ProtectAssembly();
// 3. 验证保护
VerifyProtection();
// 4. 功能测试
TestFunctionality();
}
}
12.6 持续集成最佳实践
12.6.1 缓存优化
# GitHub Actions 示例
- name: Cache .NET Reactor
uses: actions/cache@v3
with:
path: ~/.reactor
key: ${{ runner.os }}-reactor-${{ hashFiles('**/protection.nrproj') }}
12.6.2 并行构建
strategy:
matrix:
configuration: [Debug, Release]
platform: [x86, x64, AnyCPU]
steps:
- name: Protect Assembly
run: |
dotNET_Reactor.Console.exe \
-file "bin/${{ matrix.configuration }}/${{ matrix.platform }}/MyApp.exe" \
-project "protection-${{ matrix.platform }}.nrproj"
12.7 监控和报告
12.7.1 保护报告生成
# 生成 HTML 报告
dotNET_Reactor.Console.exe \
-file MyApp.exe \
-report protection-report.html \
-reportformat html
12.7.2 统计信息
Protection Statistics:
- Total Methods: 1,547
- Protected Methods: 1,234 (80%)
- Encrypted Strings: 567
- Resource Files: 23
- Protection Time: 45 seconds
- File Size: 2.5 MB → 3.1 MB (+24%)
12.8 版本控制
12.8.1 .gitignore 配置
# .gitignore
# 排除保护后的文件
bin/Protected/
obj/Protected/
# 排除临时文件
*.nrlog
*.nrtemp
# 包含配置文件
!*.nrproj
!protection-config.xml
12.8.2 配置文件管理
推荐结构:
/
├── src/
│ ├── MyApp/
│ │ ├── MyApp.csproj
│ │ └── protection.nrproj
│ └── MyLibrary/
│ ├── MyLibrary.csproj
│ └── protection.nrproj
├── config/
│ ├── protection-debug.nrproj
│ ├── protection-release.nrproj
│ └── protection-production.nrproj
└── scripts/
├── protect.bat
└── protect.ps1
12.9 本章小结
本章介绍了 .NET Reactor 的集成和自动化方案:
- Visual Studio 和 MSBuild 集成
- CI/CD 流程集成
- Docker 容器化
- 自动化测试
- 持续集成最佳实践
通过自动化集成,可以将代码保护无缝融入开发流程。

浙公网安备 33010602011771号