Microsoft Roslyn is an API that exposes the C# compiler as a service or one can say now the entire compiler is exposed in a form of a library that can be included in your project or application, earlier you needed to write code in C# and build that code, if it is successful then you get the compiled code in the form of an assembly, but now you can actually build or compile the code dynamically from within your .Net applications. You can now have Build process information or compile process information that was not available earlier. So now you can pass in your code as a string to the API and it will provide you with the code in Intermediate Language (IL) with syntactic and semantic information regarding your code. So the major advantage we get is one can do the Code Analysis using Roslyn.
Roslyn has nothing to do with the CLR, Roslyn can do the same for you as any other compiler can do, that is it can compile your code and return it in Intermediate Language that can then be passed to the CLR (as we know the CLR doesn't understand any specific language like C# or VB, so the role of every language compiler (.Net Compliant Language) is to produce a code in IL that is very well understood by the CLR).
With the introduction of Roslyn (Right now Roslyn is available a Community Technology Preview) now the compilers are no more the blackbox (that earlier takes in the code writhen in a managed language and outputs it as an assembly), now one can get the entire Syntax Tree of the code in the form of objects (API(s) are exposed to get the Syntax Tree).
One of the major advantages that I see now is that you can produce code from a User Interface, in other words suppose your application is in production and now you need to write some code, earlier you need to do the code changes and build the code again but now you can create a User Interface in your application (the UI can have a textarea and a button) and you can actually write the code on the UI and get it compiled at runtime without doing the deployment again. But yes you need to make your application intelligent enough.
Roslyn API(s) are available as NuGet Packages; you can do something anytime, so one can install it very easily by the Package Manager.
Here is a sample code showing some of the features of the Roslyn C# compiler.
First of all you need the Roslyn Libraries available as a NuGet Package that can be installed using the Package Manager in Visual Studio. For installing the Roslyn libraries, just open the Package Manager console and type in the following command:
PM> Install-Package Roslyn.Compilers.Common
Once the installation is successful, create a sample console application or a Web Application as needed and add the reference to the two libraries, that you got from the preceding installation. The two libraries are:
Roslyn.Compilers
Roslyn.Compilers.CSharp
And you are ready to go.
First we will see how to output the Syntax Tree or the code as the DLL. You can find the details of what the code does as inline comments within the code.
using System;
using System.IO;
using Roslyn.Compilers;
using Roslyn.Compilers.CSharp;
namespace SampleApplication
{
class Program
{
static void Main(string[] args)
{
//Syntax tree is the code you want to compile at runtime
var syntaxTree = SyntaxTree.ParseText(@"using System;
class TestClass
{
static void Main()
{
Console.WriteLine(""Testing Rolyn"");
Console.ReadLine();
}
}");
//Creates the copimlation, Here we are setting that compile it to a dll for the syntax tree defined above, adding references at runtime, here we are adding metadata
//reference of System library at runtime
var compilation = Compilation.Create("RoslynSampleApplication.dll",
references: new[]
{
new MetadataFileReference(typeof(object).Assembly.Location)
},
syntaxTrees: new[] { syntaxTree });
//Here we are getting the Diagnostic results for a particular syntax tree on copilation, so if there are any errors we will get a list of that errors which can be
//used to show it to user, These errors are just the same as we see in the Error list of Visual Studio.
var diagnostics = compilation.GetDiagnostics();
//Here i am prinitng the errors if any
foreach (var diagnostic in diagnostics)
{
//You can also get the line number here on which the error has occurred. Code goes something like this: diagnostic.Location.GetLineSpan(usePreprocessorDirectives: true).StartLinePosition.Line
Console.WriteLine("Error: {0}", diagnostic.Info.GetMessage());
}
//If we want to generate a DLL for the above syntax tree we can emit the compilation to a DLL by the following code.
EmitResult result;

Sajith MnPosted Mar 21, 2019, 2:38 AM
How to make it as separated class file and save it the root folder..?
David HendrickPosted Jun 15, 2015, 3:11 PM
Is it possible to add a class instance to this like Roslyn.Scripting.Session Session?
RITESH KHATRIPosted Apr 29, 2014, 5:28 AM
roslyn dll are missing
Ck NitinPosted Sep 18, 2013, 6:51 AM
Nice explanation