C# Source Generators - Write code that writes code
summary
C# 9 adds Roslyn-based source generators that run inside the compiler pipeline, receive parsed syntax trees and Compilation objects, and emit additional C# (by default in-memory) as strings that the compiler reparses and compiles. Generators must target netstandard2.0, register like analyzers, and cannot change existing source or depend on the output of other generators. Practical examples shown: auto‑notify property generation, a DI service locator that emits minimal conditionals, and a WinForms enum validator that moves reflection checks to compile time. Debugging options include GeneratorDriver, Visual Studio previews, and sourcegen.dev.
AI-written summary · about
chapters (12)
- 0:00Start
- 0:56What is a source generator?
- 8:36Potentially Frequently Asked Questions
- 13:29What can you do with them?
- 15:48Diagnostics
- 16:40Additional Files
- 17:32Usage Notes
- 20:17Referencing and Defining a Source Generator
- 22:56Reading Syntax
- 26:00Reading Symbols
- 28:24Generating Syntax
- 30:22Debugging a Source Generator
related talks
55mCode that writes code - .NET Source generators
52mCode that writes code - .NET Source generators
33mSource Generators in Action
33mMastering Source Generators
01h 01mWriting Code with Code: Getting Started with the Roslyn APIs
59mSource generators v2.0 — Incremental Generators
01h 08mAnalyzing source code using Roslyn
01h 00mAutomate Yourself Out of a Job with Roslyn
