C# Source Generators — Silent Failure After .NET 8 Update
After updating to .
- Source Generators are Roslyn components that produce C# source files at compile time.
- They run during compilation, not at runtime — zero startup cost, full type safety.
- The modern approach is IIncrementalGenerator — caches results between builds.
- Generated code is compiled into your assembly as if you wrote it manually.
- Biggest mistake: assuming generated code must use string concatenation — use SyntaxGenerator API.
Imagine you're a chef who has to hand-write the same recipe card every single day before you can start cooking. Now imagine hiring an assistant who watches what ingredients you have, then automatically prints all the recipe cards before you even walk into the kitchen. That assistant is a Source Generator — it runs before your app starts, reads your existing code, and generates new C# files so you never have to write the same boilerplate again. The generated code is real, compiled, fully typed C# — not strings, not reflection magic at runtime.
Every non-trivial C# codebase eventually drowns in boilerplate. You've written the same INotifyPropertyChanged implementation fifteen times. You've hand-rolled JSON serialization methods that drift out of sync with your models. You've copy-pasted mapping code between domain objects and DTOs until maintaining it felt like archaeology. The problem isn't laziness — it's that the language used to give you no good alternative between writing it yourself and paying the runtime cost of reflection or dynamic code generation.
Source Generators, introduced in .NET 5 and significantly improved through .NET 6, 7, and 8 with Incremental Generators, solve this at the right layer: compile time. Instead of generating code while your application runs and slowing down startup, or using T4 templates that live outside the build pipeline and break silently, Source Generators are first-class Roslyn components. They receive a full semantic model of your code, produce new C# source files, and those files are compiled into your assembly as if you wrote them yourself. Zero runtime overhead. Full IntelliSense. Full debuggability.
By the end of this article you'll understand how the Roslyn compilation pipeline hands control to your generator, how to build both a basic ISourceGenerator and the modern IIncrementalGenerator, how to handle real-world scenarios like caching, diagnostics, and multi-targeting, and — critically — the production gotchas that will bite you if you skip them. You'll walk away able to write a production-grade generator and explain it confidently in an interview.
What Are C# Source Generators?
Source Generators are components that plug into the Roslyn compiler pipeline. They receive the full compilation object — syntax trees, semantic model, references — and produce additional source files that become part of the same compilation. Unlike code generation tools that run as a separate build step (T4, custom MSBuild tasks), generators run inside the compiler. That means they have access to type information, can read attributes, and can produce code that the compiler type-checks in the same pass.
The magic happens around the CompilationUnit stage: after the compiler parses all files and binds symbols, it invokes registered generators before emitting IL. The generated files are merged into the compilation as if they were written by hand. No post-processing, no second compilation step.
The key distinction: you should never generate code that could be written as a direct method call or a generic. Generators shine when code must be repeated across types that can't share a common base class, or when the pattern depends on metadata unavailable at the generic level.
Building an Incremental Generator Pipeline
An incremental generator consists of a pipeline of steps. Each step takes an input (like a SyntaxNode or SemanticModel) and produces a value. The framework caches these values using equality comparers you provide. When a source file changes, only the affected pipeline branches re-execute.
The pipeline starts in the Initialize method where you register sources of data: CompilationProvider, SyntaxProvider, etc. The SyntaxProvider filters syntax nodes (e.g., all classes with a specific attribute). Then you transform those nodes into your model (like a DTO description), combine with the compilation, and finally register the source output.
Here's a real example that generates a TypeScript-like partial class for INotifyPropertyChanged without using reflection at runtime.
- SyntaxProvider creates an initial stream of candidate nodes.
- Select is lazy — it only runs when outputs need to be produced.
- Each step can be cached by providing a .
WithComparer()implementation. - The final RegisterSourceOutput is the action that emits generated files.
Combine() to gather cross-cutting info.Real-World Patterns: DTO Generation, Auto-Mapping, and Serialization
The most practical use of source generators is eliminating boilerplate that would otherwise require runtime reflection or code duplication. Three patterns dominate production code:
- DTO Generation from database models – Generate DTO classes with the same properties as your EF Core entities, automatically adding JsonPropertyName attributes from column names.
- Auto-Mapping – Generate extension methods that map object A to object B based on matching property names and types. No reflection, no slow runtime map building.
- Serialization helpers – Generate custom JSON serializers for specific types that avoid the overhead of System.Text.Json's reflection fallback. A source generator can produce highly optimised serialization code that outperforms the built-in serialiser by 2-3x for known types.
The pattern in all three is the same: decorate a type or member with an attribute, then the generator reads that attribute and produces the corresponding code. The generated code is a regular C# file that you can inspect and debug.
Production Pitfalls and How to Avoid Them
Source generators are powerful but they come with unique production traps. Here are the ones that bite even experienced teams:
Pitfall 1: The generator runs in a partial compilation context. You cannot call System.IO.File.ReadAllText inside a generator without first registering the file as an additional file in the csproj. The generator host restricts I/O for security and determinism.
Pitfall 2: Error reporting is silent by default. If your generator throws an unhandled exception, the build may succeed with a warning. You must report errors explicitly via context.ReportDiagnostic(Diagnostic.Create(...)).
Pitfall 3: Multi-targeting confusion. If your library targets multiple frameworks, the generator must be conditionally included only for netstandard2.0 (since it runs on the analyzer host). The generated code must also be compatible with the target framework — you can't use C# 12 syntax in code deployed to .NET Framework 4.6.1.
Pitfall 4: Assembly versioning. Generated code lives in the consuming assembly. If you update the generator package, old generated files are not automatically invalidated. Use incremental generators with versioned pipeline inputs to force regeneration when the generator version changes.
When Not to Use Source Generators
Source generators are not the right tool for every problem. They add complexity to the build process and increase assembly size. Avoid them in these scenarios:
- Simple runtime polymorphism – If a regular interface or abstract class solves the problem, don't bring a generator.
- Dynamic code that changes during runtime – Generators are compile-time only. Use Reflection.Emit or System.Linq.Expressions.
- Cross-cutting logging or tracing – Use a runtime AOP framework like Castle DynamicProxy or the .NET interceptors pattern instead of generating wrapper classes for every method.
- Small projects – The setup overhead of a generator project, package dependency, and build caching complexity is not justified for a single DTO mapping.
A good heuristic: if you can solve the problem with a generic type and a few interfaces, do that first. Only reach for a generator when the pattern repeats across unrelated types and the alternative would be runtime reflection or hand-written copy-paste.
The Generator That Silently Stopped Running After an SDK Update
- Always specify the generator attribute's language version target to ensure forward compatibility.
- Migrate to IIncrementalGenerator for production generators — it's not optional anymore.
- Add a unit test that compiles the generator and verifies output exists after each SDK version bump.
Debugger.Launch(). Enable source generator debugging via <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles> in csproj.WithComparer() on each step. Missing comparers cause full regeneration on every build.ReportDiagnostic() to surface errors. The generator exception is swallowed by the host.Key takeaways
Debugger.Launch()Common mistakes to avoid
4 patternsUsing ISourceGenerator instead of IIncrementalGenerator
Omitting [Generator(LanguageVersion)] attribute
Accessing the file system inside the generator without registering additional files
Not handling incremental caching correctly (missing comparer)
Interview Questions on This Topic
Explain the difference between ISourceGenerator and IIncrementalGenerator. When would you use each?
Frequently Asked Questions
That's C# Advanced. Mark it forged?
5 min read · try the examples if you haven't