Concept & use-cases; incremental generators (overview)
What generators do, typical use-cases, the idea of incremental generators, and a C# 6-friendly emulation using attributes + partial classes.
What is a generator?
Aim: Learn what source generators are and why they help.
- They analyze your code and generate new C# files
- Great for boilerplate (mappers, notifications, DTOs)
- Incremental idea: react only to changed inputs
- Here we emulate with attributes + partial classes (C# 6)
Partial class idea
partial classes allow splitting a type across files; generators usually emit the extra partial part.
using System;
// Partial lets code live in multiple files; generators often emit a second part.
public partial class Person
{
// "User" code (hand-written)
public string FirstName;
public string LastName;
}
// Imagine another file is generated with helpers.
public partial class Person
{
// This region simulates "generated" members.
public string FullName()
{
// simple boilerplate a generator could write
return (FirstName ?? """") + " " + (LastName ?? """");
}
}
public class Program
{
public static void Main(string[] args)
{
Person p = new Person();
p.FirstName = "Ada";
p.LastName = "Lovelace";
Console.WriteLine(p.FullName());
}
}
All lessons in this course
- Concept & use-cases; incremental generators (overview)
- Developer experience & limitations