dotnet-csharp-source-generators

Create and consume Roslyn source generators in .NET using IIncrementalGenerator.

Updated May 22, 2026
One-click install
npx skills add https://github.com/viniciuscs84/sdd-toolkit --skill dotnet-csharp-source-generators-viniciuscs84
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: dotnet-csharp-source-generators
Source: https://github.com/viniciuscs84/sdd-toolkit/tree/main/skills/dotnet-csharp-source-generators
Command: npx skills add https://github.com/viniciuscs84/sdd-toolkit --skill dotnet-csharp-source-generators-viniciuscs84

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Writing compile-time code generation in .NET is error-prone without clear patterns: developers struggle with incremental pipeline design, caching pitfalls, diagnostic reporting, and testing generators in-memory. This Skill provides concrete guidance for both authoring Roslyn source generators and consuming built-in generators like GeneratedRegex, LoggerMessage, and System.Text.Json source generation. ## Core Features & Use Cases - Generator Authoring: Build IIncrementalGenerator pipelines with syntax providers, value-equatable data models, deterministic emit patterns, and diagnostic reporting. - Testing & Debugging: Verify generator output with CSharpGeneratorDriver, snapshot testing via Verify.SourceGenerators, and emit generated files to disk for inspection. - Built-In Generator Consumption: Apply [GeneratedRegex], [LoggerMessage], and System.Text.Json source generation with [JsonSerializable] contexts for AOT-compatible serialization. - Use Case: You need a compile-time INotifyPropertyChanged implementation. Use this Skill to scaffold an incremental generator that finds attributed fields, emits partial class properties, reports diagnostics for invalid shapes, and is tested with an in-memory compilation. ## Quick Start Ask the AI to create an incremental source generator that generates properties from fields marked with a custom attribute, including a unit test using CSharpGeneratorDriver.

Frequently Asked Questions about dotnet-csharp-source-generators

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I create a source generator in C#?

Create a netstandard2.0 analyzer project referencing Microsoft.CodeAnalysis.CSharp, then implement IIncrementalGenerator with a syntax provider like ForAttributeWithMetadataName. Transform symbols into value-equatable records and emit source via RegisterSourceOutput with AddSource.

How do I test a Roslyn source generator?

Use CSharpGeneratorDriver to run the generator against an in-memory CSharpCompilation built from parsed syntax trees and metadata references. Inspect the run result's generated trees and diagnostics, or use Verify.SourceGenerators for snapshot testing.

Should I use IIncrementalGenerator or ISourceGenerator?

Use IIncrementalGenerator. It is cache-aware and only re-runs when inputs change, making it significantly faster in IDE scenarios. The legacy ISourceGenerator re-runs on every compilation.

Why does my incremental generator re-run on every keystroke?

The pipeline likely passes ISymbol or SyntaxNode objects, which break value-based caching. Transform symbols into simple record structs or strings early, since pipeline outputs are compared by value equality.

Does System.Text.Json source generation work with Native AOT?

Yes, System.Text.Json source generation with [JsonSerializable] contexts is required for Native AOT because reflection-based serialization is trimmed. Register the context via TypeInfoResolverChain in ASP.NET Core.

How do I debug a source generator in Visual Studio?

Add a Debugger.Launch call inside Initialize under a DEBUG conditional to attach a debugger, or set EmitCompilerGeneratedFiles to true in the consuming project to write generated files to disk for inspection.