【发布时间】:2012-07-19 03:06:51
【问题描述】:
刚刚接触到最新版本的 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 的,它似乎不再工作了。
有人对如何使用当前版本提出建议吗?
非常感谢。
参考资料: * Injecting a variable into the Mono.CSharp.Evaluator (runtime compiling a LINQ query from string) * http://naveensrinivasan.com/tag/mono/
【问题讨论】:
标签: mono