August 20th 2020
By Stefan Balaban, .NET Developer at Klika
Want to improve the security of your code or increase your coding speed?
Using Roslyn, the .NET compiler platform, both and more can be achieved.
The package Microsoft.CodeAnalysis and related packages provide the tools to create your own code analysis packages or to create custom boilerplate code generation.
As for code analysis there already exist many great packages that you get as an ordinary NuGet Packages.
I'll demonstrate below what kinds of analyzers exist, what they provide, and also how you can generate C# code by using Roslyn.
By using Visual Studio to write C# code, by default, you have already been using Roslyn Code Analysers. The default ones provide basic code security and typo correction.
You can add more powerful code analyzers that provide more advanced code security and code formatting options or even write your own ones if necessary.
You can find all the analyzers that a project uses under the Dependencies of a single project.
Existing analyzers are added to the project as NuGet packages. For example, I'll add the Roslynator analyzer trough the Package Manager Cosole.
Now, looking under the Analysers you will see the installed Roslynator analyzers. Under Roslynator.CSharp.Analyzers are all the rules that come with the package.
Right-clicking a rule, you can edit its severity rule — setting a rule as an error will prevent compilation if it is present.
This way you can enforce a set of rules for your code every time you build it.
Though these analyzers are not as powerful as NDepend, ReSharper, or other proprietary code analysis tools, they are free and easy to set up. Some popular analyzers are:
Another great feature of Roslyn is generating C# code.
This can be achieved by using the SyntaxGenerator class from Microsoft.CodeAnalysis.Editing package.
You can assemble an entire class by generating parts like Namespace Imports, Fields, Properties, Constructors, Methods as instances of SyntaxNode, and then creating a single Compilation Unit from those parts.
This can be used to generate classes for the underlying layers of an application. Like service a layer class with the same CRUD method implementation but for different models.
The same is achieved when using the Scaffold option in ASP.NET to generate controllers with boilerplate code.
Creating your own boilerplate can greatly increase your coding speed and increase consistency across the codebase.
Below is a small example of how this is done by generating a class with a single field, constructor, and a method for the Example model class.
Executing the code above, in a console application, returns the following:
The example shown above is just a lightweight presentation of how Roslyn can be utilized.
A more complex boilerplate generator, with custom search, create and update methods can be achieved by using attributes on your model properties to denote which of the operations can be done on which property.
From the above example, you can just add attributes in the plain text above the property however if you are reading the model class from a project like shown in the snippet below, then you can just add attributes to the project. They won't have any implementation as only their name is important.
Then get the properties with their attributes.
Afterward, you can dynamically generate SyntaxNode instances for search, create and update method bodies with whatever custom logic you need.
Also, you can generate method, constructor, getter, and setter bodies more programmatically instead of hardcoding the lines of code, as I did.
The Roslyn platform can be a powerful tool that aids during software development. Taking some time to learn to utilize it can result in a more consistent code base and quicker development time.