amis92 / csharp-source-generators

A list of C# Source Generators (not necessarily awesome) and associated resources: articles, talks, demos.
Creative Commons Zero v1.0 Universal
2.02k stars 199 forks source link
awesome-list csharp csharp-sourcegenerator dotnet list roslyn

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

Categories - [Dependency Injection (IoC Container)](#dependency-injection-ioc-container) - [Console / CLI](#console--cli) - [Mappers](#mappers) - [Communication](#communication) - [Graphics / Drawing](#graphics--drawing) - [Enums](#enums) - [Functional Programming](#functional-programming) - [Value semantic / New Type Idiom](#value-semantic--new-type-idiom) - [Immutability](#immutability) - [Discriminated Unions](#discriminated-unions) - [Serialization](#serialization) - [Json](#json) - [Localization](#localization) - [Testing](#testing) - [Mocking](#mocking) - [Patterns](#patterns) - [Mediator](#mediator) - [Command](#command) - [Builder](#builder) - [Proxy](#proxy) - [Visitor](#visitor) - [Adapter](#adapter) - [Domain Driven Design (DDD)](#domain-driven-design-ddd) - [Metaprogramming](#metaprogramming) - [Webprogramming](#webprogramming) - [Open Api](#open-api) - [Razor / Blazor](#razor--blazor) - [XAML / WPF / Avalonia](#xaml--wpf--avalonia) - [INotifyPropertyChanged](#inotifypropertychanged) - [Model View Viewmodel (MVVM)](#model-view-viewmodel-mvvm) - [Database / ORM](#database--orm) - [Statically typed resources / configurations](#statically-typed-resources--configurations) - [Other](#other)

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

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 F5 :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

Videos

Demo, PoC and excercise projects

Maybe they can inspire you too!

Projects using custom Source Generators "internally"