A set of three projects that illustrates Roslyn source generators. Enjoy this template to learn from and modify source generators for your own needs.
A .NET Standard project with implementations of sample source generators. You must build this project to see the result (generated code) in the IDE.
- SampleSourceGenerator.cs: A source generator that creates C# classes based on a text file (in this case, Domain Driven Design ubiquitous language registry).
- SampleIncrementalSourceGenerator.cs: A source generator that creates a custom report based on class properties. The target class should be annotated with the
Generators.ReportAttribute
attribute.
A project that references source generators. Note the parameters of ProjectReference
in TUnit.Core.SourceGenerator.Sample.csproj, they make sure that the project is referenced as a set of source generators.
Unit tests for source generators. The easiest way to develop language-related features is to start with unit tests.
- Use the launchSettings.json profile.
- Debug tests.
Consider installing the Roslyn syntax tree viewer plugin Rossynt.
Watch the walkthrough video: Let’s Build an Incremental Source Generator With Roslyn, by Stefan Pölz The complete set of information is available in Source Generators Cookbook.