65.9K
CodeProject is changing. Read more.
Home

Boilerplate Guide to Creating a Source Generator - Part 6

starIconstarIconstarIconstarIconstarIcon

5.00/5 (2 votes)

Jan 18, 2024

MIT

2 min read

viewsIcon

3043

A series of 6 articles to provide you with a boilerplate guide to create Source Generators.

Table of Contents

How to Use the NuGet Source Generator

There are plenty of videos and articles out there on how to import a NuGet Package that you can view, but I will show you an easy way to test locally without the need to publish it somewhere.

Setup

  • Create a location on your local hard drive. I'm using D:\Temp\NuGet.
  • In Visual Studio, select Tools -> Options -> NuGet Package Manager -> Package Sources
    • Click the "+" button in the top right corner and fill in the information:
      • Name: Local Repository
      • Source: D:\Temp\NuGet
    • Click the "Ok" button at the bottom.
    • Close all other windows.

Add the NuGet Package

  • Copy the output file from Step 5 - Packaging to the new folder.
  • Create a clone of the test application and add it to the solution:
    • Remove the reference to the Source Generator.
    • Go through the process of adding a NuGet package using the local repository as the source.
  • Shutdown Visual Studio.
  • Restart the solution.
  • Do a full rebuild all of the solution.

Your classes should still be generated and you can view them using one of the methods described in Step 3 - Implementing.

You should now be able to:

  • Uncomment the body of the PersonBuilder class, see that there is a build error.
  • Uncomment the attribute and see the error go away.
  • Right click on PersonBuilder, select "Go to definition (F12)", select the one that implements the generic Builder<t> and see what was generated.

History

  • 18th January, 2024: Initial version