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.
Microsoft recently released .NET Core 3 Preview 2 and includes more features for both .Net Core 3.0 and C# 8. .NET Core 3 will be supported in Visual Studio 2019, Visual Studio for Mac and Visual Studio Code. Visual Studio 2019 Preview 2 was released last week and has support for C# 8.
Now lets see some of the latest feature in C# 8.0:
With latest C# 8.0, you can now attache the using declaration to the scope of the current block of code and later you can dispose the object at the end. Below is the example.
One of the features which I really liked in C# 8 is the Switch Expression. Let's take the example and you will be able to understand properly.
In the above example you can see that first you don't need to declare the variable or explicit type each of the cases. Switch expressions enables terser syntax, returns a value since it is an expression, and fully integrated with pattern matching.
Code Source: Microsoft