刚刚遇到 Mono.CSharp 的最新版本,并喜欢它提供的承诺。
能够解决以下所有问题:
namespace XAct.Spikes.Duo
{
class Program
{
static void Main(string[] args)
{
CompilerSettings compilerSettings = new CompilerSettings();
compilerSettings.LoadDefaultReferences = true;
Report report = new Report(new Mono.CSharp.ConsoleReportPrinter());
Mono.CSharp.Evaluator e;
e= new Evaluator(compilerSettings, report);
//IMPORTANT:This has to be put before you include references to any assemblies
//our you;ll get a stream of errors:
e.Run("using System;");
//IMPORTANT:You have to reference the assemblies your code references...
//...including this one:
e.Run("using XAct.Spikes.Duo;");
//Go crazy -- although that takes time:
//foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
//{
// e.ReferenceAssembly(assembly);
//}
//More appropriate in most cases:
e.ReferenceAssembly((typeof(A).Assembly));
//Exception due to no semicolon
//e.Run("var a = 1+3");
//Doesn't set anything:
//e.Run("a = 1+3;");
//Works:
//e.ReferenceAssembly(typeof(A).Assembly);
e.Run("var a = 1+3;");
e.Run("A x = new A{Name=\"Joe\"};");
var a = e.Evaluate("a;");
var x = e.Evaluate("x;");
//Not extremely useful:
string check = e.GetVars();
//Note that you have to type it:
Console.WriteLine(((A) x).Name);
e = new Evaluator(compilerSettings, report);
var b = e.Evaluate("a;");
}
}
public class A
{
public string Name { get; set; }
}
}
这很有趣……可以在脚本的范围内创建一个变量,然后导出该值。
只有最后一件事要弄清楚......我如何在不使用静态的情况下获得一个值(例如,我想在其上应用规则脚本的域实体)(我正在考虑在网络应用程序中使用它) ?
我见过使用编译的委托——但那是针对以前版本的 Mono.CSharp 的,它似乎不再工作了。
有人对如何使用当前版本执行此操作有建议吗?
非常感谢。
参考:*将变量注入 Mono.CSharp.Evaluator(运行时从字符串编译 LINQ 查询) * http://naveensrinivasan.com/tag/mono/