Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#8.0

Boilerplate Guide to Creating a Source Generator - Part 4

0.00/5 (No votes)
18 Jan 2024MIT3 min read 2.6K  
A series of 6 articles to provide you with a boilerplate guide to create Source Generators.
This tutorial guides users through debugging a source generator, offering a straightforward method involving setup, launch profile configuration, and breakpoints in the source code, with insights into examining Syntax and Semantic classes, and concluding with words of wisdom about the evolving nature of source generators and the provided starting point for future refinement.

Table of Contents

Debugging the Source Generator

I've seen a couple of methods for debugging and the method I will show is by far, in my opinion, the easiest and most straightforward approach.

Setup

  • Open the source generator project file and add the following to the PropertyGroup:
    XML
    <IsRoslynComponent>true</IsRoslynComponent>
  • Close Visual Studio.
  • Re-open the solution.
  • Open the properties for the source generator project
    • Click on or scroll down to the "Debug" section.
    • Click on the "Open debug launch profiles UI".
    • Delete the existing profile.
    • Add a new profile based on "Rosyln Component".
    • In the drop down, select your test application.
  • Close all windows.

This added the profile to the Properties -> launchSettings.json file.

Debugging

  • Change the startup project to be the source generator.
  • Open the source code for the BaseGenerator.cs and set a break point at the beginning of the Initialize method.
  • Launch the debugger by pressing F5 or however you normally do.

At this point, you should hit the break point.

Preceeding at this point is normal. You have access to the watch windows and can set conditional break points.

Examining the Microsoft Classes

There are two types of classes you can examine:

  • Syntax
  • Semantic

Syntax is just that, it is the information produced by a parsing Lexar to determine if your source code is valid. Additional information can be found here: Work with syntax. If you would like to see what this looks like, in Visual Studio, select view -> Other Windows -> Syntax Visualizer. You can then click on any class definition and then begin exploring.

Semantic provides additional information about the actual classes themselves which could include attributes, interfaces, base classes etc. Additional information can be found here: Work with semantics.

These two pieces are at the heart of generation. These are big topics and are impossible to described here in this getting started tutorial.

The best I can say is, look at the object you are currently working with while debugging. You will be able to navigate up via a parent property or down via the child you are interested in.

Final Words of Wisdom

I've given you some help by providing the GetClassInformation() method in the BaseGenerator class as how to get to some of the information.

Source Generators are still new to me and therefore what I have provided is nowhere near complete for every possible thing you or I may need.

It is meant to be a starting point for me as I create more generators. I plan on continuing to refine, refactor and add helper methods in the future as I find a need for them.

History

  • 18th January, 2024: Initial version

License

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