
.NET Rocks! .NET Source Generators with Jason Bock
9 snips
Feb 5, 2026 Jason Bock, Staff software engineer and Microsoft MVP for C#, explains modern .NET source generators and why you might generate code at compile time. He covers how they work with Roslyn, practical uses like replacing reflection and aiding AOT, Microsoft’s uses (regex, P/Invoke, logging), pitfalls to avoid, and tips for building your first generator.
AI Snips
Chapters
Books
Transcript
Episode notes
Compiler-Integrated Code Generation
- Source generators run inside the Roslyn compilation pipeline and can react to code changes in the IDE during compilation.
- They generate C# code at compile time so generated artifacts participate in the same build without separate tooling.
Use Attributes As Generator Triggers
- Trigger generators off attributes and inspect the syntax/semantic model to decide what to emit.
- Generate partial types or helper classes based on existing symbols instead of runtime tricks like reflection.
Avoid Reflection Problems In AOT Builds
- Source generation replaces runtime reflection patterns with compile-time code, improving compatibility with AOT scenarios.
- AOT and runtime reflection clash, so compile-time generation prevents runtime trimming surprises.

