Source generators are a new feature in .NET 5 and later versions that allow developers to write code that generates source code at build time. They can be used to automate repetitive code generation tasks or to provide compile-time optimizations that were previously only possible at runtime.
The idea behind source generators is to provide a way for developers to generate code at build time without the need for external tools or libraries. This makes it possible to generate code that is tailored to the specific needs of an application, without the need to ship a separate code generator or runtime library.
Source generators are implemented as C# source code files that are compiled along with the rest of the project. They can be triggered by an attribute or a method call in the source code, and they have access to the compilation context, which provides information about the project being built.
Developers can use source generators to generate code in any .NET language, including C# and Visual Basic. The generated code can be added to the project as a new source file or injected into an existing source file.
Source generators are a powerful tool for .NET developers, and they can be used to solve a wide range of problems. For example, they can be used to generate strongly-typed client libraries for web APIs, to generate serialization and deserialization code for complex data structures, or to generate code that implements a specific design pattern or architecture.
Overall, source generators are a welcome addition to the .NET ecosystem, and they are sure to become a valuable tool for developers in the years to come.
I have been using them 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. I will also write about other source generators that I find useful.
Microsoft .net sOURCE gENERATORS
oTHER sOURCE gENERATORS
TBA
For a list of other source generators along with info on how to build your own, go to: https://github.com/amis92/csharp-source-generators
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/
Upcoming Articles
- TBA
Discover more from dotNetTips.com
Subscribe to get the latest posts sent to your email.
