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

UnreliableCode Community

Developer Research, Reverse Engineering & Coding Community

Question

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

PacketSniffer
Network Protocols & Sockets
MEMBER
Rep: 195
Join Date: Sep 2024
Posts: 22
Thanks: 49
1y ago · Jun 25, 2025 10:33 AM
#1
I want to write a source generator that scans classes for a custom attribute and automatically generates serialization boilerplate. Where do I start?
PacketSniffer | Network Protocols & Winsock SPI
The following users thanked PacketSniffer for this post:
ParanormalDev
MelonLoader / BepInEx Dev
MEMBER
Rep: 230
Join Date: Sep 2023
Posts: 21
Thanks: 58
1y ago · Jun 25, 2025 3: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!
ParanormalDev | MelonLoader & ImGui Modding
CodeVoyager
C++ / Systems Dev
MEMBER
Rep: 57
Join Date: Apr 2023
Posts: 7
Thanks: 39
1y ago · Jun 25, 2025 4:14 PM
#3
Check out the Microsoft.CodeAnalysis.CSharp NuGet package. Incremental generators are blazingly fast and keep IDE typing responsive.
CodeVoyager · C++ / Systems Dev
Exploring modern C++20/23, template metaprogramming, and Lin...