Monday, January 18, 2016

.NET Compiler Platform "Roslyn"

At PicScout, we use .NET Compiler Platform, better known by its codename "Roslyn", 
an open source compiler and code analysis API for C#.

The compilers are available via the traditional command-line programs and also as APIs, which are available natively from within .NET code.

Roslyn exposes modules for syntactic (lexical) analysis of code, semantic analysis, dynamic compilation to CIL, and code emission.

Recently, I used Roslyn for updating hundreds of files to support an addition to our logging framework.
I needed to add a new member in each class that was using the logger and modify all the calls to the logger, of which there were several hundreds.

We came up with two ways of handling this challenge.

After reading a class into an object representing it, one possible way of adding a member is to supply a syntactical definition of a new member, and then re-generate back the source code for the class.
The problem with this approach was the relative difficulty of configuring a new member correctly.

Here is how it might look:

Generating the code:
private readonly OtherClass _b = new OtherClass(1, "abc");

Another option, which is more direct, was to simply get the properties of the class and use them.
For example, we know where the class definition ends and we can append a new line containing the member definition.

Here is how it looks:

Get class details:





Insert the new line (new member):



After that, replacing the calls to the new logger is a simple matter of search - replace.


2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Verbatim transcription involves the documenting information that is stored in audio form. In this type of transcription, all the remarks and speech patterns that are considered to be distracting during other transcriptions are documented. This means that things like coughing, pauses and laughter also need to be noted. If there are some background noises in the tape being used for the process, they have to be documented too. this site

    ReplyDelete