What is Deconstructed Compiler ? How C# gains a dynamic language's ability to generate and invoke code at runtime via Roslyn?

91 views Asked by At

After reading this article about Roslyn. I came across two things that i did not understand.

  • deconstructed compiler
  • C# gains a dynamic language's ability to generate and invoke code at runtime via Roslyn

I have searched a lots of posts on stack overflow and googled for it. but could not get the whole picture.

Can anyone please explain to me or direct me with some links and resources about these topics.

1

There are 1 answers

4
Matías Fidemraizer On

Taken from the linked article:

Hejlsberg demonstrated a C# program that passed a few code snippets to the C# compiler as strings; the compiler returned the resulting IL assembly code as an object, which was then passed to the Common Language Runtime (CLR) for execution. Voilà! With Roslyn, C# gains a dynamic language's ability to generate and invoke code at runtime.

The part of:

[...] C# gains a dynamic language's ability to generate and invoke code at runtime.

...is just a very wrong assumption made by the blog post author...

  1. Compiling code from an application doesn't turn C# into a dynamic language or it doesn't turn new C# compiler in a substitute of an interpreter...

  2. C# was able to generate code at run-time since its inception using Reflection Emit. It seems like the new compiler didn't add that feature, but anyway it's easier to generate code from regular C# code with the new compiler than using Reflection Emit. In addition, as @hvd has noted in some comment, it was also possible since C# inception using CSharpCodeProvider.

  3. C#, since .NET 4.0, can interoperate with dynamic languages using the Dynamic Language Runtime, which was created to open the door to interpreted language implementations on top of .NET (and also to make COM interop easier...).

About the other question (the thing about deconstructed compiler), it means that the new C# compiler allows you to hook other code to perform actions based on C# compilation steps.

I would take a look at Roslyn Overview on GitHub where there're a lot of details that should give more depth on the topic.