Source Generators were introduced in .NET 5 and provide a powerful way to not only generate code but also improve performance. Microsoft describes them this way:
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 compilation. In other words, you can provide additional source code as input to a compilation while the code is being compiled.https://learn.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/source-generators-overview
I have been using them ever since they came out and plan to write my own that coincides with the coding standards in my book “Rock Your Code: Coding Standards for Microsoft .NET” available on Amazon.com.
Since the .NET team does not list all the source generators that come out of the box, I have decided to write an article on each one that I can. I have asked the team to provide a list, but until then, I hope you will check back to this post often.
Have you found a source generator that you really like, please send me info and I will possibly add them to this list. You can email me by going here: https://dotnettips.wordpress.com/about/
- Regular Expression Source Generator
- DLL Library Import