Click here to Skip to main content
15,887,267 members
Articles / Programming Languages / C# 8.0

Boilerplate Guide to Creating a Source Generator - Part 6

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
18 Jan 2024MIT2 min read 2.7K   2  
A series of 6 articles to provide you with a boilerplate guide to create Source Generators.
This tutorial provides a straightforward method for local testing of a NuGet Source Generator, guiding users through the setup of a local repository, adding the NuGet package, and verifying generated classes within a test application, emphasizing steps to uncomment code, observe build errors, and explore generated code via the Visual Studio environment.

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

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Software Developer (Senior) Webbert Solutions
United States United States
Dave is an independent consultant working in a variety of industries utilizing Microsoft .NET technologies.

Comments and Discussions

 
-- There are no messages in this forum --