Trimming & Reflection Limitations
Handle trim warnings, use [DynamicallyAccessedMembers], and replace runtime reflection with source-generated alternatives.
What Is IL Trimming?
IL Trimming is a publish-time optimization that removes unused types, methods, and assemblies from the output. The result is a smaller executable. It is automatically enabled for Native AOT and self-contained single-file publishes.
Enabling Trimming
Enable trimming in the project file. You can control the trim mode from partial (trim unused assemblies only) to full (trim unused members within assemblies).
// .csproj
<PropertyGroup>
<!-- Enable trimming on publish -->
<PublishTrimmed>true</PublishTrimmed>
<!-- full: aggressive — trims unused members within assemblies
partial: conservative — only removes unused assemblies -->
<TrimMode>full</TrimMode>
<!-- Treat all trimmer warnings as errors (recommended for CI) -->
<TrimmerRootAssemblies>MyApp</TrimmerRootAssemblies>
<SuppressTrimAnalysisWarnings>false</SuppressTrimAnalysisWarnings>
</PropertyGroup>
// Publish:
dotnet publish -c Release -r linux-x64 --self-containedAll lessons in this course
- Native AOT Compilation
- Trimming & Reflection Limitations
- ReadyToRun & Tiered Compilation
- Benchmarking with BenchmarkDotNet