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

UnreliableCode Community

Developer Research, Reverse Engineering & Coding Community

Question

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

CppModernist
Modern C++ Advocate
MEMBER
Rep: 379
Join Date: Jan 2020
Posts: 6
Thanks: 90
7mo ago · Dec 25, 2025 2:39 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?
CppModernist · Modern C++ Advocate
Writing idiomatic C++20/23: ranges, modules, std::format, an...
The following users thanked CppModernist for this post:
HexEditorDev
Low-Level Hacker
VIP
Rep: 129
Join Date: May 2024
Posts: 6
Thanks: 33
7mo ago · Dec 25, 2025 5:14 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!
HexEditorDev · Low-Level Hacker
Inspecting raw byte streams, endianness swapping, and bitwis...
FastMath
SIMD Math Library
MEMBER
Rep: 49
Join Date: Dec 2025
Posts: 6
Thanks: 71
7mo ago · Dec 26, 2025 6:14 PM
#3
Check out the Microsoft.CodeAnalysis.CSharp NuGet package. Incremental generators are blazingly fast and keep IDE typing responsive.
FastMath · SIMD Math Library
Header-only C++ matrix math library author. Fast vectors and...