Home

Awesome

C# Source Generators


A list of C# Source Generators (not necessarily awesome), because I haven't found a good list yet.

C# Source Generators is a Roslyn compiler feature introduced in C#9/.NET 5. It lets C# developers inspect user code and generate new C# source files that can be added to a compilation.

Add GitHub topic csharp-sourcegenerator to your generator repo - let's get it started!

Documentation and samples

Source Generators

<details open> <summary>Categories</summary> </details> <!-- Sorted alphabetically in each category. Template for entries: - [REPO](https://github.com/REPO) - ![stars](https://img.shields.io/github/stars/REPO?style=flat-square&cacheSeconds=604800) ![last commit](https://img.shields.io/github/last-commit/REPO?style=flat-square&cacheSeconds=86400) -->

Dependency Injection (IoC Container)

Console / CLI

Mappers

Communication

Graphics / Drawing

Enums

Functional Programming

Value semantic / New Type Idiom

Immutability

Discriminated Unions

Serialization

Json

Localization

Testing

Mocking

Patterns

Mediator

Command

Builder

Proxy

Visitor

Adapter

Domain Driven Design (DDD)

Metaprogramming

Webprogramming

Open Api

Razor / Blazor

XAML / WPF / Avalonia

INotifyPropertyChanged

Model View Viewmodel (MVVM)

Database / ORM

Statically typed resources / configurations

Text templating

Other

Meta - libs and generators for other generators

<!-- Sorted alphabetically. Template for entries: - [REPO](https://github.com/REPO) - ![stars](https://img.shields.io/github/stars/REPO?style=flat-square&cacheSeconds=604800) ![last commit](https://img.shields.io/github/last-commit/REPO?style=flat-square&cacheSeconds=86400) -->

Tips & Tricks

Collection of tips and tricks (simple and brief to fit in Tweet):

Tweeted by @raboof on May 25 2021 at 23:00:

TIL to debug a source generator in VS 16.10: upgrade Microsoft.CodeAnalysis.CSharp to 3.10.*, add <IsRoslynComponent>true</IsRoslynComponent> to source generator project, select Roslyn Component for Launch in Project Properties Debug page, choose Target then <kbd>F5</kbd> :rocket:

Source Generator debugger

Tweeted by @raboof on Nov 16 2020 at 20:57:

See files emitted by #SourceGenerators by adding these properties to your (*.csproj) project file:

<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)Generated</CompilerGeneratedFilesOutputPath>

Tweeted by @Chiser99 on Sep 02 2020 at 06:41:

I made a thing: https://github.com/chsienki/Kittitas

If you're building Roslyn Source Generators or Analyzers check it out, it makes debugging them inside the compiler easier. #roslyn #csharp #dotnetcore #sourcegenerators #analyzers

Build failed in WPF projects microsoft/CsWin32#7:

If your build failed in a _wpftmp.csproj file you need to add following property to your (.csproj) project file:

<IncludePackageReferencesDuringMarkupCompilation>true</IncludePackageReferencesDuringMarkupCompilation>

and use at least .NET 5.0.102 SDK

Reference local projects or embed NuGet packages to source generator assemblies dotnet/roslyn#47517:

âš  Please be aware that this may result in crashes, when another generator or SDK component loads such an assembly with lower version. If you can, please avoid embedding additional DLLs/packages.

You can add a dependency to a source generator in the same solution through three steps:

  1. Add a <PackageReference>, making sure to set both GeneratePathProperty="true" and PrivateAssets="all"
  2. Add a build target to add <TargetPathWithTargetPlatformMoniker> elements as part of GetTargetPath, and add all the required dependency assemblies inside this target, making sure to set IncludeRuntimeDependency="false"
  3. Update <GetTargetPathDependsOn> to ensure the target from the previous step is used

You can see an example of these steps here: https://github.com/dotnet/roslyn-sdk/blob/0313c80ed950ac4f4eef11bb2e1c6d1009b328c4/samples/CSharp/SourceGenerators/SourceGeneratorSamples/SourceGeneratorSamples.csproj#L13-L30

Articles

<!-- Sorted from newest. Please follow the template: - [Title](URL) (YYYY-MM-DD) short description. -->

Videos

<!-- Sorted from newest. Please follow the template: - [Title](URL) (YYYY-MM-DD) short description. -->

Demo, PoC and excercise projects

Maybe they can inspire you too!

Projects using custom Source Generators "internally"