Code Mahek
  • Blog
  • About
  • Contact
  • Random Clicks
  • Resume
  • Ava Chat Bot

Fragrance
​of Programming

C# source generators

5/9/2020

Comments

 
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:​
Picture
Credit: Microsoft
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
Picture
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:​
Picture
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.
Picture
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:
Picture
Now if you compile and run the above console, you will get the below output:
Picture
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

    RSS Feed

    Categories

    All
    Azure
    C#
    Cloud
    CodeProject
    CSharp
    DotNet Core
    Jetbrains
    .NET
    Rider
    Source Generators
    SQL
    Tech News
    Ubuntu
    Visual Studio 2015
    Visual Studio 2019
    Windows
    Windows 10

    Archives

    May 2020
    February 2019
    January 2019
    December 2018
    May 2018
    December 2017
    November 2017
    October 2017
    September 2017
    July 2017
    May 2017
    April 2017
    May 2015
    April 2015
    March 2015

    View my profile on LinkedIn

Location

Contact Us

    Subscribe Today!

Submit
  • Blog
  • About
  • Contact
  • Random Clicks
  • Resume
  • Ava Chat Bot