Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add references using latest Roslyn API (C# Script Execution)

Tags:

c#

roslyn

I'm a little confused as to how to go about adding references when using Roslyn to execute C# scripts.

I'm using the latest version of the API (1.2.20906.2), installed via NuGet.

I've searched lots of posts on Google, but there is significant change in the API since many of the examples I've found.

To illustrate what I'm trying to achieve:

using System;
using Roslyn.Scripting.CSharp;

namespace Test.ConsoleApp
{
    public class Program
    {
        static void Main(string[] args)
        {
            new ScriptRunner().RunScripts();
        }
    }

    public class ScriptRunner
    {
        public void RunScripts()
        {
            var engine = new ScriptEngine();

            var session = engine.CreateSession();

            session.AddReference("System");
            session.AddReference("System.Linq");

            // The following script runs successfully

            session.Execute(@"using System;

                              var arr = new[] {1, 2, 6};

                              foreach (var i in arr)
                              {
                                  if(i > 1)  
                                  {
                                    Console.WriteLine(i);
                                  }
                              }"
                           );

            // The following script using Linq fails

            session.Execute(@"using System;
                              using System.Linq;

                              var arr = new[] {1, 2, 6};
                              var arrResult = arr.Where(x => x > 1);

                              foreach (var i in arrResult)
                              {
                                  Console.WriteLine(i);
                              }"
                           );

            Console.ReadLine();
        }
    }
}

UPDATE - Included modification suggested in answer, plus referencing by path to DLL:

using System;
using Roslyn.Scripting.CSharp;

namespace Test.ConsoleApp
{
    public class Program
    {
        static void Main(string[] args)
        {
            new ScriptRunner().RunScripts();
        }
    }

    public class ScriptRunner
    {
        public void RunScripts()
        {
            var engine = new ScriptEngine();

            var session = engine.CreateSession();

            session.AddReference("System");
            session.AddReference("System.Core"); // This reference is required to use Linq language features
            session.AddReference("System.Linq");

            session.Execute(@"using System;
                              using System.Linq;

                              var arr = new[] {1, 2, 6};
                              var arrResult = arr.Where(x => x > 1);

                              foreach (var i in arrResult)
                              {
                                  Console.WriteLine(i);
                              }"
                           );

            // Example use of adding reference to external project by path to dll

            session.AddReference(@"E:\SVN\CSharpRoslynTesting\CSharpRoslynTesting\Test.ExternalLibraryTest\bin\Debug\Test.ExternalLibraryTest.dll");

            session.Execute(@"using System;
                              using Test.ExternalLibraryTest;

                              var externalTestClass = new ExternalTestClass();
                              externalTestClass.TestOutput();
                            "
                           );

            Console.ReadLine();
        }
    }
}
like image 221
gb2d Avatar asked Nov 20 '13 13:11

gb2d


1 Answers

It is working for me, although I am using v1.2.20906.1. I didn't try your code, I just executed version I wrote for Roslyn presentation one month back.

Try to add System.Core:

session.AddReference("System.Core");

That's the only significant difference I can see so far.

Update: I just tried your code and missing reference I mentioned above was indeed the culprit. You even get nice exception:

(5,51): error CS1061: 'int[]' does not contain a definition for 'Where' and no extension method 'Where' accepting a first argument of type 'int[]' could be found (are you missing a using directive or an assembly reference?)

like image 168
Tomas Voracek Avatar answered Oct 21 '22 16:10

Tomas Voracek