One of the new features in .NET 5 that triggered my curiosity where source generators. I did some investigation on how to use them and want to share the lessons I learned along the way.
But let’s first start with an explanation what “a source generator” actually is:
A Source Generator is a new kind of component that C# developers can write that lets you do two major things:
- Retrieve a Compilation object that represents all user code that is being compiled. This object can be inspected and you can write code that works with the syntax and semantic models for the code being compiled, just like with analyzers today.
- Generate C# source files that can be added to a Compilation object during the course of compilation. In other words, you can provide additional source code as input to a compilation while the code is being compiled.
So in short, source generators are a new compiler feature that allow you to inspect existing code and generate new code(remark: you cannot change existing code).
I started with the example mentioned in the blog post above:
But when I build my project, nothing happened and it looked like no code was generated. What was I doing wrong?
How to debug a source generator?
So the first question became how can I debug a source generator? Now I had no clue how to investigate what was (not) happening.
The trick is to add a `Debugger.Launch()` statement inside your code:
Now when you build your code a popup should appear that asks you how you what debugger you want to launch.