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

UnreliableCode Community

Developer Research, Reverse Engineering & Coding Community

Question

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

ZeroMemory
Member
MEMBER
Rep: 75
Join Date: Oct 2024
Posts: 32
Thanks: 18
3y ago · Sep 7, 2022 9:48 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?
ZeroMemory · Always experimenting
The following users thanked ZeroMemory for this post:
RustMechanic
Unity & IL2CPP Reverser
VIP
Rep: 295
Join Date: Nov 2022
Posts: 32
Thanks: 74
3y ago · Sep 7, 2022 1:28 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!
RustMechanic | IL2CPP & Unity Engine Analysis
NoRecoilDev
Input & Recoil Math
MEMBER
Rep: 130
Join Date: Nov 2024
Posts: 20
Thanks: 32
3y ago · Sep 8, 2022 6:28 PM
#3
Check out the Microsoft.CodeAnalysis.CSharp NuGet package. Incremental generators are blazingly fast and keep IDE typing responsive.
NoRecoilDev | Smoothing Algorithms & Input Events