0

以前我写了一个程序,使用 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中实现。”

请帮忙

4

1 回答 1

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 在评论中建议的列表:

codeLine += "var numbers = new List<int>(new int[] {1,2,3,4,5,6,7,8,9,10});";
于 2012-06-19T12:56:13.827 回答