Roslyn Constructor Magic

Mostly, we are doing a line to line translation. E.g. every line in Python code roughly translates into a line of C#, preserving order. However, there are differences between Python and C# semantics, that, unfortunately, force SharPy to perform more complicated transformations.

For example, implementations of __init__ method may call parent class __init__ anywhere in the body. This is not so for C# and base constructor calls, which have a form of

ClassName(): base(...){...}

As you can see, the base constructor call is separeted from the derived class constructor body with a special syntax, and has to be called first. That means one must make a potentially complicated syntax tree transform to convert one to another. Fortunately, C# compiler API codenamed Roslyn was made exactly to do this, and is here to rescue.

Read More