Home / Forums / How do C# Source Generators work and how can I build one? [Discussion #8]

UnreliableCode Community

Developer Research, Reverse Engineering & Coding Community

Question

How do C# Source Generators work and how can I build one? [Discussion #8]

StructAligner
Data-Oriented Design
VIP
Rep: 301
Join Date: Dec 2025
Posts: 8
Thanks: 25
8mo ago · Dec 7, 2025 8:15 PM
#1
I want to write a source generator that scans classes for a custom attribute and automatically generates serialization boilerplate. Where do I start?
StructAligner · Data-Oriented Design
Structure of Arrays (SoA) vs Array of Structures (AoS) for m...
The following users thanked StructAligner for this post:
OptiMax
Benchmark Specialist
MEMBER
Rep: 95
Join Date: Apr 2023
Posts: 6
Thanks: 31
8mo ago · Dec 7, 2025 9:24 PM
#2
Source Generators hook into the Roslyn compiler via ISourceGenerator or IIncrementalGenerator (preferred in modern .NET). IIncrementalGenerator caches syntax node inputs so it only re-generates code when files with your attribute actually change!
OptiMax · Benchmark Specialist
BenchmarkDotNet, micro-optimizations, and zero-GC hot paths....
TypeSystemNerd
Type Theory & CLR
MEMBER
Rep: 314
Join Date: Nov 2024
Posts: 6
Thanks: 57
8mo ago · Dec 8, 2025 11:24 PM
#3
Check out the Microsoft.CodeAnalysis.CSharp NuGet package. Incremental generators are blazingly fast and keep IDE typing responsive.
TypeSystemNerd · Type Theory & CLR
Covariance, contravariance, struct boxing, and value type se...