是否可以解释 C# 表达式树以发出 JavaScript?
例如,如果您有这样的表达式:
Expression<Func<int, int>> fn = x => x * x;
是否有任何东西可以遍历表达式树并生成它?
"function(x) { return x * x; }"
For example, if you have an expression like this:
Expression<Func<int, int>> fn = x => x * x;
Is there anything that will traverse the expression tree and generate this?
"function(x) { return x * x; }"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
C# 编译器已经为您解析了该表达式;剩下的就是遍历表达式树并生成代码。可以递归地遍历树,并且可以通过检查每个节点的类型来处理它(
Expression
有多个子类,表示例如函数、运算符和成员查找)。每种类型的处理程序可以生成适当的代码并遍历节点的子节点(根据其表达式类型,这些代码将在不同的属性中可用)。例如,可以通过首先输出“function(”,后跟参数名称,后跟“) {”来处理函数节点。然后,可以递归地处理正文,最后输出“}”。The expression has already been parsed for you by the C# compiler; all that remains is for you to traverse the expression tree and generate the code. Traversing the tree can be done recursively, and each node could be handled by checking what type it is (there are several subclasses of
Expression
, representing e.g. functions, operators, and member lookup). The handler for each type can generate the appropriate code and traverse the node's children (which will be available in different properties depending on which expression type it is). For instance, a function node could be processed by first outputting "function(" followed by the parameter name followed by ") {". Then, the body could be processed recursively, and finally, you output "}".一些人开发了开源库来寻求解决这个问题。我一直在研究的是Linq2CodeDom,它将表达式转换为CodeDom图,然后可以将其编译为JavaScript 只要代码兼容即可。
Script# 利用原始 C# 源代码和编译后的程序集,而不是表达式树。
我对 Linq2CodeDom 进行了一些小的编辑,以将 JScript 添加为受支持的语言 — 本质上只是添加对 Microsoft.JScript 的引用、更新枚举,并在GenerateCode 中添加一个案例。下面是转换表达式的代码:
下面是结果:
方法签名反映了 JScript 更强类型的本质。最好使用 Linq2CodeDom 生成 C#,然后将其传递给 Script# 以将其转换为 JavaScript。我相信第一个答案是最正确的,但正如您通过查看 Linq2CodeDom 源代码所看到的,在处理每种情况以正确生成代码方面需要付出很多努力。
A few people have developed open source libraries seeking to solve this problem. The one I have been looking at is Linq2CodeDom, which converts expressions into a CodeDom graph, which can then be compiled to JavaScript as long as the code is compatible.
Script# leverages the original C# source code and the compiled assembly, not an expression tree.
I made some minor edits to Linq2CodeDom to add JScript as a supported language--essentially just adding a reference to Microsoft.JScript, updating an enum, and adding one more case in GenerateCode. Here is the code to convert an expression:
And here is the result:
The method signature reflects the more strongly-typed nature of JScript. It may be better to use Linq2CodeDom to generate C# and then pass this to Script# to convert this to JavaScript. I believe the first answer is the most correct, but as you can see by reviewing the Linq2CodeDom source, there is a lot of effort involved on handling every case to generate the code correctly.
这可能并不容易,但是,是的,这绝对可行。像 Entity Framework 或 Linq to SQL 这样的 ORM 可以将 Linq 查询转换为 SQL,但实际上您可以从表达式树中生成任何您想要的内容...
您应该实现一个
ExpressionVisitor
来分析和转换表达式。编辑:这是一个适用于您的示例的非常基本的实现:
它仅使用单个指令处理 lambda,但无论如何 C# 编译器无法为块 lambda 生成表达式树。
当然,还有很多工作要做,这是一个非常小的实现...您可能需要添加方法调用 (
VisitMethodCall
)、属性和字段访问 (VisitMember) 等
It's probably not easy, but yes, it's absolutely feasible. ORMs like Entity Framework or Linq to SQL do it to translate Linq queries into SQL, but you can actually generate anything you want from the expression tree...
You should implement an
ExpressionVisitor
to analyse and transform the expression.EDIT: here's a very basic implementation that works for your example:
It only handles lambdas with a single instruction, but anyway the C# compiler can't generate an expression tree for a block lambda.
There's still a lot of work to do of course, this is a very minimal implementation... you probably need to add method calls (
VisitMethodCall
), property and field access (VisitMember
), etc.Microsoft 内部开发人员使用 Script# 来执行此操作。
Script# is used by Microsoft internal developers to do exactly this.
看一下 Lambda2Js,这是一个由 Miguel Angelo 就是为了这个目的。
它向任何表达式添加了一个
CompileToJavascript
扩展方法。示例 1:
示例 2:
更多示例此处。
Take a look at Lambda2Js, a library created by Miguel Angelo for this exact purpose.
It adds a
CompileToJavascript
extension method to any Expression.Example 1:
Example 2:
More examples here.