Code that writes code - .NET Source generators
summary
Source generators hook into the Roslyn pipeline (must target .NET Standard 2.0) to produce ephemeral .g.cs files at compile time, removing reflection and runtime cost and giving full IntelliSense. Examples build working features: incremental generators that avoid IDE lag, generated strongly typed ID structs with implicit conversions, a combined service-registration extension from attributes, options binding driven by an external schema via the additional files provider, and compile‑time mapping diagnostics. Code is tested with Roslyn-based unit tests and snapshot verification and emitted via register source output.
AI-written summary · about
chapters (9)
- 0:00Introduction to .NET source generators
- 3:06Why use source generators
- 7:30Source generators and compiler
- 10:20Generating strongly typed IDs
- 22:18Automating service registration
- 29:31Compiling config options
- 36:21Generating object mappers
- 45:43Testing and debugging generators
- 50:41Key takeaways and resources
related talks
52mCode that writes code - .NET Source generators
59mC# Source Generators - Write code that writes code
33mSource Generators in Action
33mMastering Source Generators
59mSource generators v2.0 — Incremental Generators
01h 01mWriting Code with Code: Getting Started with the Roslyn APIs
59mAutomating Observability With Roslyn Source Generators
01h 00mAutomate Yourself Out of a Job with Roslyn
