4 ways to generate code in C# — Including Source Generators in .NET 5

Xiaodi Yan
Level Up Coding
Published in
20 min readJan 19, 2021

--

Microsoft introduced Source Generators in the latest C# version. It is a new feature that allows us to generate source code when the code compiles. In this article, I will walk you through four ways that can generate source code to simplify our daily jobs. Then you can choose the proper way for various scenarios.

Good developers are lazy developers because they don’t want to repeat the code over and over again. In the .NET world, we have such ways to help us generate code:

--

--