Linq IronPython中的表达式树

Linq IronPython中的表达式树,linq,ironpython,expression-trees,Linq,Ironpython,Expression Trees,我使用此代码使用IronPython执行python表达式 ScriptEngine engine = Python.CreateEngine(); ScriptScope scope = engine.CreateScope(); scope.SetVariable("m", mobject); string code = "m.ID > 5 and m.ID < 10"; ScriptSource source = engin

我使用此代码使用IronPython执行python表达式

    ScriptEngine engine = Python.CreateEngine();
    ScriptScope scope = engine.CreateScope();      
    scope.SetVariable("m", mobject);
    string code = "m.ID > 5 and m.ID < 10";
    ScriptSource source = 
engine.CreateScriptSourceFromString(code, SourceCodeKind.Expression);
    source.Execute(scope);
ScriptEngine=Python.CreateEngine();
ScriptScope scope=engine.CreateScope();
scope.SetVariable(“m”,mobject);
字符串代码=“m.ID>5和m.ID<10”;
ScriptSource源=
CreateScriptSourceFromString(代码,SourceCodeKind.Expression);
来源、执行(范围);
是否有办法将生成的表达式树作为c#对象获取,例如
块表达式

IronPython的内部AST也恰好是表达式树,所以您只需要获取代码的AST,您可以使用类来完成。Parser.ParseFile方法将返回表示代码的实例

使用解析器有点棘手,但是您可以查看_ast模块的。基本上是:

Parser parser = Parser.CreateParser(
    new CompilerContext(sourceUnit, opts, ThrowingErrorSink.Default),
    (PythonOptions)context.LanguageContext.Options);

PythonAst ast = parser.ParseFile(true);
ThrowingerLink
也来自
\u ast
模块。您可以像这样获得一个
SourceUnit
实例(c.f.):


然后,您必须遍历AST以从中获得有用的信息,但它们应该与C#表达式树相似(但不完全相同)。

您将代码传递到哪里?我已经参考了Microsoft脚本库和IronPython。Iam使用.net 4.0这里的
上下文是什么?@Asad:CodeContext实例。我相信您还可以从ScriptEngine获得LanguageContext实例。
SourceUnit sourceUnit = context.LanguageContext.CreateSnippet(source, filename, SourceCodeKind.Statements);