dotnet-csharp-source-generators

Create and use Roslyn source generators in .NET with CSharpGeneratorDriver.

1|Updated Feb 22, 2026
One-click install
npx skills add https://github.com/rudironsoni/dotnet-agent-harness --skill dotnet-csharp-source-generators-rudironsoni
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: dotnet-csharp-source-generators
Source: https://github.com/rudironsoni/dotnet-agent-harness/tree/main/.rulesync/skills/dotnet-csharp-source-generators
Command: npx skills add https://github.com/rudironsoni/dotnet-agent-harness --skill dotnet-csharp-source-generators-rudironsoni

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

This Skill provides comprehensive guidance on creating and consuming Roslyn source generators, enabling developers to automate code generation and improve build-time performance in .NET projects.

Core Features & Use Cases

  • Create Custom Generators: Learn to implement IIncrementalGenerator for efficient, cache-aware code generation.
  • Utilize Built-in Generators: Understand how to leverage [GeneratedRegex], [LoggerMessage], and System.Text.Json source generation.
  • Testing & Debugging: Master techniques for testing generators with CSharpGeneratorDriver and debugging strategies.
  • Use Case: Automatically generate boilerplate code for data transfer objects (DTOs), implement custom serialization logic, or enforce coding standards at compile time.

Quick Start

Use the dotnet-csharp-source-generators skill to create a basic incremental source generator that adds a INotifyPropertyChanged implementation to fields marked with a custom attribute.

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 Roslyn source generator in .NET?

To create a Roslyn source generator in .NET, you implement the IIncrementalGenerator interface to enable efficient, cache-aware code generation. This approach allows you to automatically generate boilerplate code during compilation.

What is the difference between incremental source generators and standard generators in .NET?

Incremental source generators in .NET provide a cache-aware approach to code generation, improving build-time performance by only re-executing when necessary. Standard generators run unconditionally, whereas incremental generators optimize the pipeline.

How do I test a C# source generator using CSharpGeneratorDriver?

You test a C# source generator using CSharpGeneratorDriver to execute the generator within a test harness. This methodology allows you to verify the generated syntax trees and ensure diagnostic reporting behaves as expected.

Can I use built-in .NET source generators like GeneratedRegex and LoggerMessage for AOT compatibility?

Yes, you can use built-in .NET source generators like GeneratedRegex and LoggerMessage for compile-time code optimization. These generators pre-compile logic, which significantly enhances Ahead-of-Time (AOT) compatibility by eliminating runtime reflection.

How do I report diagnostics from an incremental generator in Roslyn?

You report diagnostics from an incremental generator in Roslyn by registering a diagnostic descriptor within the generation pipeline. This allows the compiler to surface custom warnings or errors directly in the IDE based on analyzed syntax.

What are common limitations when using Roslyn source generators for compile-time code generation?

Roslyn source generators cannot modify existing code and are limited to adding new syntax trees during compilation. They also require strict separation from user project dependencies, meaning you cannot use runtime APIs within the generator itself.