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

UnreliableCode Community

Developer Research, Reverse Engineering & Coding Community

Question

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

DisasmGeek
Capstone & Keystone Specialist
MEMBER
Rep: 160
Join Date: Dec 2024
Posts: 21
Thanks: 38
1y ago · Jun 16, 2025 5:57 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?
DisasmGeek · Capstone / Keystone Engine Integration
The following users thanked DisasmGeek for this post:
NeighborSpy
Unity & Secret Neighbor Modder
VIP
Rep: 290
Join Date: May 2023
Posts: 17
Thanks: 76
1y ago · Jun 16, 2025 10:23 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!
NeighborSpy · Secret Neighbor & Unity Netcode
PlayerRole, Keycards, Basement Locks & Stamina inspection
SharpShooter_x
Unity C# Modder
MEMBER
Rep: 234
Join Date: May 2024
Posts: 8
Thanks: 29
1y ago · Jun 17, 2025 3:23 AM
#3
Check out the Microsoft.CodeAnalysis.CSharp NuGet package. Incremental generators are blazingly fast and keep IDE typing responsive.
SharpShooter_x · Unity C# Modder
Deep diving into Unity script lifecycles, IL2CPP internals, ...