> Markdown version of [/videos/100154-code-that-writes-code-net-source-generators?t=2](https://www.wearedevelopers.com/videos/100154-code-that-writes-code-net-source-generators?t=2). Every page supports `.md` or `Accept: text/markdown`. Links point to the HTML versions so they work for humans too. Agent guide: [/agents.md](https://www.wearedevelopers.com/agents.md). --- # Code that writes code - .NET Source generators Tired of writing repetitive boilerplate or relying on slow runtime reflection? Discover how .NET source generators hook into the compiler to automatically write AOT-compatible C# for you. - **Speakers:** [Glenn F. Henriksen](https://www.wearedevelopers.com/@glenn-f-henriksen) - **Event:** World Congress 2026 Europe - **Published:** July 9, 2026 - **Duration:** 29:55 - **URL:** https://www.wearedevelopers.com/videos/100154-code-that-writes-code-net-source-generators ## Summary Tired of manually writing repetitive boilerplate or relying on slow, runtime reflection? .NET source generators offer a powerful, deterministic solution by hooking directly into the Roslyn compilation pipeline. By implementing the `IIncrementalGenerator` interface, developers can inspect their codebase or external files during compilation and output precise, newly generated C# source code. This approach not only eliminates tedious manual work but naturally leads developers into the "pit of success," effortlessly handling repetitive tasks like registering dependencies, mapping objects, or implementing strongly typed IDs. Creating a performant source generator involves a careful lifecycle: utilizing a lightweight syntax predicate to scout for target nodes (like custom attributes), transforming that data into a minimal cached model, and finally emitting the generated source. This incremental architecture strictly minimizes work, keeping the IDE fast. Practical applications are vast—from parsing non-C# JSON or schema files to auto-generate configuration classes, to mapping objects safely at compile-time instead of runtime. Because generators can also analyze semantic models, developers can easily emit custom compile-time diagnostic errors, stopping the build instantly if a property mapping is missing or incompatible. While navigating the Roslyn API can seem daunting, verifying custom generators doesn't require attaching multiple Visual Studio instances. Using snapshot testing frameworks like Verify makes debugging generation logic straightforward through standard unit tests. Ultimately, writing custom incremental generators helps organizations codify their unique business rules directly into the compiler, while built-in .NET tools demonstrate how shifting reflection to compile-time generation vastly improves application performance and Ahead-of-Time (AOT) compatibility. **Keywords:** .net source generators, roslyn compiler API, incremental code generation, compile-time diagnostics, boilerplate code automation, iincrementalgenerator implementation, c# partial classes, syntax tree filtering, roslyn semantic models, strongly typed entity ids, AOT compilation support, runtime reflection alternative, snapshot testing generators, custom build errors, external file code parsing ## Chapters 1. **Understanding the basics of .NET source generators** (00:02) — Hook into the Roslyn compiler pipeline to deterministically generate C# code based on existing syntax trees. 1. **Implementing a basic source generator pipeline** (02:42) — Create an IIncrementalGenerator implementation to register and output generated source code files automatically during build time. 1. **Generating strongly typed identifiers to replace boilerplate** (05:44) — Use partial structs and the Roslyn syntax tree to automatically generate boilerplate comparison and serialization code. 1. **Generating C# code from external configuration files** (12:27) — Utilize the AdditionalTextProvider approach to inspect and generate native code directly from external unmanaged files. 1. **Raising build errors with custom diagnostic messages** (16:06) — Validate object mapping constraints at compile time by analyzing semantics and reporting explicit diagnostic build errors. 1. **Testing and debugging source generator output files** (20:36) — Verify generated code structures reliably using snapshot testing instead of relying purely on fragile debugger attachment setups. 1. **Best practices and optimization tips for compilation** (25:38) — Maintain fast incremental compilation phases by keeping search predicates short and caching minimally sized transform models. 1. **Replacing runtime reflection with native compilation features** (26:41) — Replace runtime reflection bottlenecks with compile-time steps and explore framework features that leverage generated components natively. ## Related Moments - [Overview of C# source generators and compilation pipeline](https://www.wearedevelopers.com/videos/192-c-9-source-generators-let-the-machine-do-the-programming) (from "C# 9 Source Generators - let the machine do the programming") - [Common use cases for source code generation scenarios](https://www.wearedevelopers.com/videos/192-c-9-source-generators-let-the-machine-do-the-programming) (from "C# 9 Source Generators - let the machine do the programming") - [Addressing iterative code generation and developer challenges](https://www.wearedevelopers.com/videos/1354-google-gemma-and-open-source-ai-models-clement-farabet) (from "Google Gemma and Open Source AI Models - Clement Farabet") - [Using code generation to enable smaller development teams](https://www.wearedevelopers.com/videos/100297-the-impact-of-ai-on-game-development-and-the-industry) (from "The Impact of AI on Game Development and the Industry") - [Accelerating software engineering velocity with automated code generation](https://www.wearedevelopers.com/videos/595-fireside-chat-with-sir-tim-berners-lee) (from "Fireside Chat with Sir Tim Berners-Lee") - [Generative artificial intelligence and programming fundamentals](https://www.wearedevelopers.com/videos/1291-using-all-the-html-running-state-of-the-browser-and-modern-is-rubbish) (from "Using all the HTML, Running State of the Browser and "Modern" is Rubbish") ## Related Articles - [Liuba Gonta and Yuliya Khadasevic - GitHub Copilot Beyond the Basics - 10 Ways to Elevate Your Coding](https://www.wearedevelopers.com/magazine/490-liuba-gonta-and-yuliya-khadasevic-github-copilot-beyond-the-basics-10-ways-to-elevate-your-coding) - [How to Use Generative AI to Accelerate Learning to Code](https://www.wearedevelopers.com/magazine/530-how-to-use-generative-ai-to-accelerate-learning-to-code) - [One billion (bad?) developers: How AI is changing the way we learn to code](https://www.wearedevelopers.com/magazine/516-one-billion-bad-developers-how-ai-is-changing-the-way-we-learn-to-code) - [Transforming Software Development: The Role of AI and Developer Tools](https://www.wearedevelopers.com/magazine/527-transforming-software-development-the-role-of-ai-and-developer-tools) ## Related Jobs - [AI Software Engineer (Germany)](https://www.wearedevelopers.com/jobs/48317-ai-software-engineer-germany) at **Sunhat** - [Senior Software Engineer](https://www.wearedevelopers.com/jobs/ext/15942-senior-software-engineer) at **GitHub** - [Senior Software Engineer, Enterprise Products](https://www.wearedevelopers.com/jobs/ext/1841248-senior-software-engineer-enterprise-products) at **GitHub** - [Staff Software Engineer, Copilot Experiences](https://www.wearedevelopers.com/jobs/ext/164361-staff-software-engineer-copilot-experiences) at **GitHub** - [Principal Software Engineer, Identity](https://www.wearedevelopers.com/jobs/ext/1469181-principal-software-engineer-identity) at **GitHub** - [Principal Software Engineer, Enterprise AI Platform](https://www.wearedevelopers.com/jobs/ext/1467292-principal-software-engineer-enterprise-ai-platform) at **GitHub**