.NET Core has a feature for "trimming unused code", but because it is implemented using static analysis, its trimming effect is not optimal. It has the following two disadvantages:
It does not support Windows Forms and WPF, yet desktop application developers are actually the ones with the strongest demand for the trimming feature.
It cannot remove assemblies that are not used at runtime. For example, suppose our program uses assembly A, which in turn references assemblies B and C. In assembly A, only method M1 uses assembly B, and only method M2 uses assembly C. Our program only calls method M1 in A and never calls method M2 in A. Although assembly C has never been called by our program, since the "trimming unused code" feature only performs static reference checks, assembly C will still not be trimmed away.
It does not handle reflection well. Because it uses static analysis, it may trim away assemblies that are only loaded via reflection at runtime.
Therefore, I developed an application for trimming .NET Core programs, which solves the issues mentioned above with .NET Core's "trimming unused code" feature. It supports Windows Forms and WPF. It analyzes the assemblies loaded by the program at runtime to determine which assemblies are unused. As a result, it can remove more unused assemblies and naturally supports reflection.
Trimming effect comparison:
| Original Size | .NET Built-in Trimming | Zack.DotNetTrimmer | |
|---|---|---|---|
| Empty Core MVC | 97MB | 50.3MB | 43.6MB |
| Empty WebAPI | 93MB | 46.3MB | 34.5 MB |
| Empty WPF | 152 MB | Not supported | 75.2 MB |
| Empty WinForms | 152 MB | Not supported | 50.0 MB |
Project open source address: https://github.com/yangzhongke/Zack.DotNetTrimmer/