使用Roslyn June CTP 2012生成动态LINQ时出错

时间:2012-06-19 12:52:39

标签: c# roslyn

有时候我用Mono编译器作为服务编写了一个程序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mono.CSharp;

namespace MonoTest
{
    class Program
    {
        static void Main(string[] args)
        {
            //Initializing the evaluator
            Evaluator.Init(new string[0]);           

            //Importing namespaces 
            Evaluator.Run("using System;");
            Evaluator.Run("using System.Linq;");
            Evaluator.Run("using System.Collections.Generic;");

            //Sum of 'n' numbers
            Evaluator.Run("List<int> numbers= new List<int> {1,2,3,4,5,6,7,8,9,10} ;");
            Evaluator.Run("var sum = numbers.Sum();");
            Evaluator.Run("Console.WriteLine(sum);");
        }
    }
}

//输出

55

现在尝试使用 Roslyn (我的第一个代码)[Roslyn项目类型:控制台应用程序]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Roslyn.Scripting;
using Roslyn.Scripting.CSharp;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            var host = new ScriptingHost();
            string codeLine = string.Empty;
            codeLine += "List<int> numbers= new List<int> {1,2,3,4,5,6,7,8,9,10} ;" + Environment.NewLine;
            codeLine += "var sum = numbers.Sum();";
            codeLine += "Console.WriteLine(sum);";

            var res = host.Execute(codeLine);

            //Write the result back to console
            if (res != null)
                Console.WriteLine(" = " + res.ToString());

            Console.ReadKey();

        }
    }

    public class ScriptingHost
    {
        private ScriptEngine engine;
        private Session session;       

        public ScriptingHost()
        {
            //Create a session
            session = Session.Create(this);

            //Create the engine, just pass the assemblies and namespaces
            engine = new ScriptEngine(new Assembly[]
                                {
                                    typeof(Console).Assembly,
                                    typeof(ScriptingHost).Assembly,
                                    typeof(IEnumerable<>).Assembly,
                                    typeof(IQueryable).Assembly
                                },
                                new string[] 
                                { 
                                    "System", "System.Linq", 
                                    "System.Collections",
                                    "System.Collections.Generic"
                                }
                            );

        }

        //Pass the code to the engine, nothing much here
        public object Execute(string code)
        {
            return engine.Execute(code, session); --Error at this line
        }
    }
}

但是收到此错误

“(1,34):错误CS8000:此语言功能(带有初始化程序的'对象创建表达式)尚未在Roslyn中实现。”

请帮忙

1 个答案:

答案 0 :(得分:4)

看起来您必须将列表的创建与填充列表分开:

codeLine += "List<int> numbers= new List<int>();";
codeLine += "var values = new int[] {1,2,3,4,5,6,7,8,9,10};";
codeLine += "numbers.AddRange(values);";

编辑:或使用@svick在评论中建议的constructor overload which takes a list

codeLine += "var numbers = new List<int>(new int[] {1,2,3,4,5,6,7,8,9,10});";