This tutorial instructs users to create a .NET Standard 2.0 Class Library for a source generator, emphasizing specific project modifications, addition of necessary NuGet packages, and providing a basic infrastructure with objects like BaseClass, Comparers, Models, and BaseGenerator to facilitate the creation of a reusable source generator for multiple projects, with optional modifications outlined for customization.
Introduction
Looking to create a Source Generator and don't know where to begin? This 6-part article will provide you with a boilerplate guide that will guide you through the process.
Table of Contents
Create the Basic Infrastructure of the Source Generator
Create the Source Generator Library
Create a new .NET Standard 2.0 Class Library and add it to your existing solution.
WARNING: It is very important that you create it as a .NET Standard 2.0 Class Library. If you do not, the generator will not run.
WARNING: Changing the library type after creation has led to odd results. Best to delete project and start over before you get too far.
Project Modifications
Add the Basic Infrastructure
Take a look at the source code provided.
These objects layout the basic structure for any source source generator.
You will see the following structure:
BaseClass
- Comparers
ClassInformationComparer
GeneratorInformationComparer
- Models
ClassInformation
GeneratorInformation
PropertyInformation
- BaseGenerator
RandomTestDataGenerator
These objects are my attempt to start to normalize the creation of a basic infrastructure that can be re-used for multiple projects. While the names are unimportant, the general structure and contents will be important.
Models and comparers are necessary in order to prevent unnecessary thrashing of the generator, so do not skip using them.
Making Modifications
BaseGenerator
- This largely could be left alone. If you don't plan on using the GeneratorInformation
class, then you will need to do a search on GeneratorInformation
and replace it with the class name you plan on using.
The parts you might need to change:
If you don't intend to use my BaseGenerator
class, you will need to derive from IIncrementalGenerator
. At this point, I highly recommend leaving both: Initialize
and IsSyntaxTarget
in whole. This is boilerplate stuff that you definitely need.
RandomTestDataGenerator - Change the following:
- Name of the class to your generator name
- Class namespace
- Constant values
The attribute [Generator]
is very much needed, so leave it in place.
History
- 18th January, 2024: Initial version