大家好,我是编程乐趣。
https://github.com/MichalStrehovsky/SeeSharpSnake
dotnet publish -r win-x64 -c Release
2、添加参数/p:PublishTrimmed=true,移除未使用的代码,大小为25MB
dotnet publish -r win-x64 -c Release /p:PublishTrimmed=true
3、使用CoreRT的编译模式,大小为4.7MB
dotnet publish -r win-x64 -c Release /p:Mode=CoreRT
dotnet publish -r win-x64 -c Release /p:Mode=CoreRT-Moderate
dotnet publish -r win-x64 -c Release /p:Mode=CoreRT-High
dotnet publish -r win-x64 -c Release /p:Mode=CoreRT-ReflectionFree
dotnet publish -r win-x64 -c Release /p:Mode=CoreRT-NoRuntime
8、采用csc.exe编译,大小为8KB
//编译C#代码
csc.exe /debug /O /noconfig /nostdlib /runtimemetadataversion:v4.0.30319 MiniRuntime.cs MiniBCL.cs Game\FrameBuffer.cs Game\Random.cs Game\Game.cs Game\Snake.cs Pal\Thread.Windows.cs Pal\Environment.Windows.cs Pal\Console.Windows.cs Pal\Console.cs /out:zerosnake.ilexe /langversion:latest /unsafe
//将库组合成一个可执行文件
link.exe /debug:full /subsystem:console zerosnake.obj /entry:__managed__Main kernel32.lib ucrt.lib /merge:.modules=.rdata /merge:.pdata=.rdata /incremental:no /DYNAMICBASE:NO /filealign:16 /align:16
以上就是作者尝试的编译精简过程,我们使用比较多是第2步,其他步骤可能会有兼容问题,实际项目要慎用。
但是.Net 7开始支持AOT,还能进一步优化!大家感兴趣的可以自行尝试下。