是LINQ表达式树适宜树种? [英] Are LINQ expression trees proper trees?

查看:125
本文介绍了是LINQ表达式树适宜树种?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是LINQ表达式树适宜树种,如,图表(定向与否,维基百科似乎并不太同意)无环?什么是从下面的C#表达式的表达式树的根

 (字符串s)=> s.Length 



表达式树是这个样子,用 - >表示该属性的名称节点的另一个节点是通过访问

   - 方式>参数[0] 
LAMBDA ---- -----参数(字符串s)
\ /
\->车身/ - >表达
\ /
会员(长)

在使用ExpressionVisitor参观LambdaExpression的ParameterExpression被访问两次。有使用ExpressionVisitor访问LambdaExpression,使所有的节点访问恰好一次的方式,并在一个特定的,公知的订单(订单预,按顺序,后序等)?

解决方案

排序的,是的。实际的主干(如果你愿意)一个 LambdaExpression 。体;参数是对树的结构(和它需要的东西),但 .Parameters 上方(你的虚线)所需的元数据是不是真的树的一部分函数图像 - 只有当这些节点在树中,他们是有趣的,因为价值换人的实际身体以后使用。



ParameterExpression 被访问两次是必不可少的,这样就可能有人交换的参数,如果他们想要的 - 例如,建立一个完整的新的 LambdaExpression 具有相同数目的参数,但不同的参数实例(也许改变的类型)。



的顺序将是相当稳定的,但应考虑一个实现细节。例如,给定一个节点,如添加(A,B),它应该没有语义差异是否我参观了 A -first VS b 负一。


Are LINQ expression trees proper trees, as in, graphs (directed or not, wikipedia does not seem too agree) without cycles? What is the root of an expression tree from the following C# expression?

(string s) => s.Length

The expression tree looks like this, with "->" denoting the name of the property of the node the other node is accessible through.

     ->Parameters[0]
 Lambda---------Parameter(string s)
    \               /
     \->Body       /->Expression
      \           /
      Member(Length)

When using ExpressionVisitor to visit the LambdaExpression, the ParameterExpression is visited twice. Is there a way to use the ExpressionVisitor to visit the LambdaExpression so that all the nodes are visited exactly once, and in a specific, well-known order (pre-order, in-order, post-order etc.)?

解决方案

Sort of, yes. The actual "trunk" (if you will) of a LambdaExpression is the .Body; the parameters are necessary metadata about the structure of the tree (and what it needs), but .Parameters at the top (your dotted line) isn't really part of the tree's functional graph - it is only when those nodes are used later in the actual body of the tree that they are interesting, as value substitutions.

The ParameterExpression being visited twice is essential, so that it is possible for someone to swap the parameters if they wanted - for example, to build an entire new LambdaExpression with the same number of parameters, but different parameter instances (maybe changing the type).

The order will be fairly stable, but should be considered an implementation detail. For example, given a node such as Add(A,B), it should make no semantic difference whether I visit that A-first vs B-first.

这篇关于是LINQ表达式树适宜树种?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆