Fragrance
|
One of the most exciting upcoming feature in .NET 5 is the Source Generators. As the name suggests Source Generators, can inspect a program and generate source files that can be added to a compilation. Microsoft says Source Generators can improve performance in a number of scenarios. Source Generators enables you to dynamically add the new code as a part of the build process, e.g. adding new classes dynamically. Also, to be noted is that Source Generators is designed to generate the new code but can't modify the existing code which completely make sense. As per the Microsoft, Source generators run as a phase of compilation visualized below: Now lets go on for sample coding, below are the steps you need to follow as of now: Step 1: Creating the Source Generator First step is to create the "Source Generator" which you can do by creating the separate new project. Before that, to use "Source Generator", you need to install Visual Studio Preview and .NET 5 preview. Once installed, you need to create the new Project as shown below Once the project is created, you’ll need to modify the project file by double clicking on it. Source Generators are currently in preview so expect better tooling support in the final versions. Change the project file to the following: After modifying the project file, make sure its building fine with no errors. Step 2: Define Source Generator Now lets do some actual coding and define a Source Generator. Add a new class called “MySourceGenerator” and implement the ISourceGenerator interface and decorate the class with [Generator] attribute – both of these are from the Microsoft.CodeAnalysis namespace: In this simple example I will just add a new class to the compilation. I will just add a simple class without any logic involved just to get the basic understanding. In the "Execute" function we have "SourceGeneratorContext" which helps us to add the source to the compilation. Anyways, build the above code and still Source generation is not taken place. Step 3: Register Source Generator Now create a new .Net core console project to the solution and name it as "EmployeeConsole". After creating the project, add project reference for "SourceGenerator" and we even need to modify the Console project file as shown below. To opt-in to the code generation, the EmployeeConsole project file needs to modified to add <LangVersion>preview</LangVersion> and change the reference to the generator project to be an analyzer. After making the above change make sure you build the project and there are no errors reported. Step 4: Play with the Generated Code After it's successfully compiled, in Program.cs file you can use the generated namespace i.e. "EmployeeGenerator". In the main method now I can use the class "Employee" just like a regular class and even refer all its Properties and Functions. Add the below code and you will even see that Visual Studio is smart enough to show the intellisense for all the generated code. Below is the Program.cs file: Now if you compile and run the above console, you will get the below output: Above sample is a very simple example but at least you will get the idea how exactly Source Generator works. Real world scenario is very complex and I am sure Source Generators will be very useful.
Days are not far when you will be generate the class from the external file and also from the Database based on the condition. Please let me know your thoughts about Source Generators. Keep reading and keep sharing.
Comments
|
Categories
All
Archives
May 2020
|