Module 9: Source Generators (The Auto-Writer)
📚 Module 9: Source Generators
Course ID: DOTNET-106
Subject: The Auto-Writer
Reflection (Module 6) lets you inspect code at Runtime. Source Generators allow you to write code at Compile-time.
🏗️ Step 1: The “Write it for Me” Problem
Imagine you have 100 classes, and every single one needs a ToString() method that prints all its properties.
- Junior way: You write the method 100 times by hand.
- Senior way: You write a Generator that writes those 100 methods for you while you sleep.
🧩 The Analogy: The Stamp Press
- Reflection is like a person looking at a document and manually typing it.
- Source Generators are like a Stamp Press. You build the stamp once, and the machine stamps every piece of paper perfectly as it passes through the factory (The Compiler).
🏗️ Step 2: Zero-Runtime Cost
Because the code is generated before the app even runs, it is just as fast as if you had written it yourself. There is zero “Reflection” penalty.
🥅 Module 9 Review
- Source Generator: A tool that writes extra C# code while you are compiling.
- Compile-time: The period when your code is being turned into an app.
- Performance: Significantly faster than Reflection.