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!
- dotnet/roslyn feature design document describing the compiler feature.
- dotnet/roslyn cookbook to help with generator creation.
- dotnet/roslyn-sdk samples show how to implement a source generator and use features like external package references (inside generators). Includes AutoNotify, Csv, Maths, Mustache, and SettingsXml.
- sourcegen.dev - an online Source Generator Playground to play with generator ideas 💡 without any setup noise. Source repo.
- davidwengier/SourceGeneratorTemplate - A basic template for writing a C# source generator, from the Roslyn dev.
- AutoDeconstructable - Generator for efficient and automatic flat text serializer/deserializer using Deconstructable aspect in NTP library.
- AutoInterface - interface-to-member source generator.
- Avalonia.NameGenerator - Generates typed references to named Avalonia XAML controls.
- avatar - A modern compile-time generated interception/proxy library.
- Azura - Generates binary [de]serializers on Streams at design time.
- boilerplatezero - WPF Dependency Property Generator.
- Cloneable - auto-generate Clone method.
- CrossLink - A C# Library for creating and managing multiple links between objects.
- Data Builder Generator - Generate data builder patterns for your model classes.
- DevExpress.Mvvm.CodeGenerators - Generates boilerplate code for your View Models (INotifyPropertyChanged, Commands, IDataErrorInfo, DevExpress services).
- dotVariant - A type-safe and space-efficient sum type for C# (comparable to discriminated unions in C or C++)
- Fairy - generates C# code based on Sitecore Content Serialization (SCS)
.yml
files. - Generator.Equals - generates equality and hashing for classes and records, supports a number of strategies for comparing collections and properties.
- Imp.NET - - a fast, high-level, object-oriented C# networking library that supports the invocation of remote methods through proxy interface objects.
- IoTHubClientGenerator - Build a C# Azure IoT Device client program in seconds!
- JsonByExampleGenerator - - generate classes based on example json files in your project.
- JsonSrcGen - - compile time JSON serializer generation.
- lambdajection - Framework for building AWS Lambdas using dependency injection and aspect-oriented programming.
- MapTo - - A convention based object to object mapper similar to Automapper.
- MemberAccessGenerator - generates
GetMember(int)
and/orGetMember(string)
methods that return property value for a given property name or index (e.g. in positional records). - MiniRazor - Portable Razor compiler & code generator.
- MockableStaticGenerator - A C# source generator to make an interface and a class wrapper to test static/extension methods.
- MockGen - A C# mocking library based on source generators.
- MockSourceGenerator - A C# mocking library that generates mocks at compile-time using a source generator.
- net_automatic_interface - .Net Core Source Generator for Automatic Interfaces.
- PrimaryConstructor - Generate primary constructor from readonly fields.
- PrimitiveStaticDataGenerator - for creating methods that return optimized
ReadOnlySpan<T>
static data from primitive values. - PrintMembersGenerator - helps re-defining C# record's PrintMembers method to force include/exclude certain members.
- ResXFileCodeGenerator - Generates strongly-typed resource classes for looking up localized strings.
- ScenarioTests - Test your code like you would write a notebook. Simply and effective
- SmallSharp - Create, edit and run multiple C# 9.0 top-level programs in the same project by just selecting the startup program from the start button.
- SmartAnnotations - A library that uses source generators to automatically generate data annotations for your models. It provides a strongly-typed mechanism (fluent like API) to define your annotation rules.
- SourceInject - A source generator that allow you to generate your services for dependencies injection during compile time.
- SpreadCheetah - Create Excel files with a C# Source Generator for generating the rows.
- SqlMarshal - Native AOT friendly-performant mini-ORM. Generation of wrappers for accessing SQL using ADO.NET.
- StackXML - Stack based zero-allocation XML serializer and deserializer.
- StringLiteralGenerator - for optimizing UTF-8 binaries.
- StrongInject - - compile time dependency injection for .NET.
- StructPacker - binary serializer that auto-generates C# serialization code to achieve peak runtime performance and efficiency.
- Svg to C# Source Generators - SVGC compiles SVG drawing markup to C# using SkiaSharp as rendering engine. SVGC can be also used as codegen for upcoming C# 9 Source Generator feature.
- ThisAssembly - Exposes project and assembly level information as constants in the ThisAssembly class.
- ToString - - C# source generator for implementing
ToString
override likerecord
type. - Transplator - A simple C# source generator for text templates.
- Tinyhand - - Tiny and simple data format/serializer using a source generator.
- ValueChangedGenerator - for generating PropertyChanged from inner struct members.
- ValueObjectGenerator - C# source generator is for ValueObjects (ie.Wrapper classes).
- WrapperValueObject - - for creating simple value objects wrapping primitive types.
Collection of tips and tricks (simple and brief to fit in Tweet):
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
- Using C# Source Generators to create an external DSL (2021-01-27) that shows how to implement a simple DSL.
- 4 ways to generate code in C# — Including Source Generators in .NET 5 (2021-01-19) demonstrates the comparison between Source Generators, T4 template and Reflection, etc.
- .NET 5 Source Generators - MediatR - CQRS - OMG! (2020-12-16) explores how source generators can be used to automatically generate an API for a system using the MediatR library and the CQRS pattern.
- Source Generators in .NET 5 with ReSharper (2020-11-20) introduces source generators and briefly mentions how they are being worked into the ReSharper product.
- Source Generators - real world example (2020-11-09) contains a rich and deep dive into a real world generator with lots of useful tips.
- How to profile C# 9.0 Source Generators (2020-10-10) demonstrates how to profile your source generator using the performance profiling tools built into Visual Studio.
- How to Debug C# 9 Source Code Generators (2020-10-09) contains debugging tips.
- How to generate code using Roslyn source generators in real world scenarios (2020-09-17) rich story of how ThisAssembly generator was written using Scriban templates.
- .NET Blog 'New C# Source Generator Samples' post (2020-08-25) that shows some simple samples.
- .NET Blog 'Introducing C# Source Generators' post (2020-04-29) that announces the feature.
- C# Source Generators - Write code that writes code - David Wengier (2020-11-13) Roslyn dev takes deep dive into the topic.
- .NET Languages and Runtime Community Standup - Source Generators (2020-10-08) shows how Generators work and how they can be tested.
- Channel 9 'Source Generators in C#' (2020-08-12) has Roslyn PMs discussing the feature.
Maybe they can inspire you too!
- AutoCoder - for effective and flexible object coding.
- RyanAlameddine/SourceGeneratorDemo - contains 3 demos: hello world, INPC and OpCode class.
- TMC-CSharp/CodeExerciseLibrary - Library to help creating C# exercises. Generates missing methods and classes inside tests on the fly by using Source Generators.
- DpdtInject - DI container based on C# Source Generators. Its goal is to remove everything possible from runtime and make resolving process as faster as we can. This is achieved by transferring huge piece of resolving logic to the compilation stage into the source generator.
- jakubsturc/talk-csharp-source-generators - contains 4 generators: AutoNotify, ConsoleWritelineHijack, HelloWorld and SettingsXml, plus nice presentation slides.
- Compile Time Method Execution Generator - proof of concept that allows executing a method during compilation, so that it can be really fast during runtime.
- NetFabric.Hyperlinq - generates overloads for its extension methods.
- RestEase - uses Source Generator to generate interface implementations on compile time instead of in runtime via Reflection.Emit.
- WarHub/wham - generates code for immutable tree object graph based on red-green node approach used in Roslyn; generates custom XmlSerializer that supports C#9 records and ImmutableArray.