Question

I would like to ask you for explanation of how to use the roslyn as a service to integrate it in the application that I developed?

Also, how could I use it as compiler that take code form user and produce the output?

I have searched a lot but I can't understand that

I started already with a simple solution but there are some errors:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Roslyn.Scripting;
using Roslyn.Scripting.CSharp;
namespace RoslynTest
{
    class Program
    {
        static void Main(string[] args)
        {
            ScriptEngine engine = new ScriptEngine();
            Session session = Session.Create();
            engine.Execute("using system;", session);
            engine.Execute("for(int i=0;i<10; i++) console.writeLine(i*1); ", session);
        }
    }
}

The errors are:

Error 1 'Roslyn.Scripting.Session' does not contain a definition for 'Create'

Error 2 'Roslyn.Scripting.CSharp.ScriptEngine' does not contain a definition for 'Execute' and no extension method 'Execute' accepting a first argument of type 'Roslyn.Scripting.CSharp.ScriptEngine' could be found (are you missing a using directive or an assembly reference?)

Was it helpful?

Solution

You're doing it all wrong.

First you'll need to add the references to the appropriate compiler assemblies.

Roslyn.Compilers.dll
Roslyn.Compilers.CSharp.dll

Add any using lines you may want.

using Roslyn.Scripting.CSharp;

Then create the engine, your session and run your code.

var engine = new ScriptEngine();
var session = engine.CreateSession();
session.Execute("using System;");
session.Execute("for (int i = 0; i < 10; i++) Console.WriteLine(i);");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top