我正在寻找一个C#脚本引擎,可以在维护上下文的同时解释C#代码块。例如,如果输入:var a = 1;
,然后a + 3
,则输出4
。
我知道MS Roslyn
,确实这样做,但它是一个沙盒(相对于启动它的程序)。所以,如果我创建ScriptEngine
的实例和MyClass
的实例(只是我的一个arbirary类),我没有选择将my_class
的引用传递给{{1} }。
是否有可能以某种方式传递该引用?
我想要做什么,就像:
script_engine
请注意ScriptEngine engine; // A Roslyn object
Session session // A Roslyn object
MyClass my_class; // My object
// all required initializations
Submission<object> sm = session.CompileSubmission<object>("var a=1;");
dynamic result = sm.Execute();
Submission<object> sm = session.CompileSubmission<object>("a + 3;");
dynamic result = sm.Execute(); // result is now 4
MyClass my_class;
session.AddReferenceToAnOject(my_class); // function that does not exists, but reflect my intention
Submission<object> sm = session.CompileSubmission<object>("my_class.ToString();");
dynamic result = sm.Execute(); // result is no the output of my_class.ToString()
是缺失的部分,因为在roslyn中没有这样的功能。
答案 0 :(得分:11)
在@Herman评论的link中找到答案。
事实证明,Roslyn ScriptEngine/Session
支持Host Object
的概念。
为了使用它,定义一个选择的类,并在创建会话时传递它。这样做,使该主机对象的所有 public 成员可用于会话内的上下文:
public class MyHostObject
{
public List<int> list_of_ints;
public int an_int = 23;
}
var hostObject = new MyHostObject();
hostObject.list_of_ints = new List<int>();
hostObject.list_of_ints.Add(2);
var engine = new ScriptEngine(new[] { hostObject.GetType().Assembly.Location });
// passing reference to hostObject upon session creation
var session = Session.Create(hostObject);
// prints `24` to console
engine.Execute(@"System.Console.WriteLine(an_int + list_of_ints.Count);",
session);
答案 1 :(得分:0)
这是一个如何将变量从object传递给roslyn动态执行代码的完整过程。包含的还有eval()包装器。 首先从microsoft.com http://www.microsoft.com/en-sa/download/details.aspx?id=34685
安装RoslynSetup.exe然后将参考Roslyn.Compilers和Roslyn.Compilers.CSharp添加到您的项目(程序集/扩展)
这是vs2012中C#console app的完整工作代码。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Roslyn.Compilers;
using Roslyn.Compilers.CSharp;
using Roslyn.Scripting;
using Roslyn.Scripting.CSharp;
namespace testRoslyn
{
class Program
{
static void Main(string[] args)
{
TestRoslyn tr = new TestRoslyn();
tr.Test = "this was set from main program ";
tr.test();
System.Console.WriteLine(tr.Test);
tr.Test = "this was set from main program for eval";
Eval e = new Eval();
e.create<TestRoslyn>(tr);
e.eval("Test = Test + \" AND THIS WAS SET FROM Eval()\";");
System.Console.WriteLine(tr.Test);
string a = e.eval<string>("string a = \"return this from eval\";a");
System.Console.WriteLine(a);
tr.Test = "now return this";
string b = e.eval<string>("string a = Test + \" ... and this\";a");
System.Console.WriteLine(b);
double d = e.eval<double>("double dbl = 1.2345*3;dbl");
System.Console.WriteLine(d);
e.eval("string testIt(string a){return \"testIt(): \"+a+\"\";}");
string c = e.eval<string>("string c = testIt(\"nice\");c");
System.Console.WriteLine(c);
Console.ReadKey();
}
}
public class TestRoslyn
{
public string Test;
public TestRoslyn()
{
}
public string test()
{
ScriptEngine roslynEngine = new ScriptEngine();
Roslyn.Scripting.Session session = roslynEngine.CreateSession(this);
session.AddReference(this.GetType().Assembly);
session.AddReference("System.Web");
session.ImportNamespace("System");
session.ImportNamespace("System.Web");
var result = (string)session.Execute("Test = Test + \" ... and this was set from roslyn code.\";Test");
return result;
}
}
public class Eval
{
ScriptEngine RoslynEngine = new ScriptEngine();
Roslyn.Scripting.Session Session;
public void create<T>(T hostObject = null) where T : class
{
RoslynEngine = new ScriptEngine();
Session = RoslynEngine.CreateSession(hostObject);
if (hostObject != null)
Session.AddReference(hostObject.GetType().Assembly);
Session.AddReference("System.Web");
Session.ImportNamespace("System");
Session.ImportNamespace("System.Web");
}
public void eval (string strEval)
{
Session.Execute(strEval);
}
public T eval<T>(string strEval)
{
return (T) Session.Execute(strEval);
}
}
}