什么是表达式树,如何使用它们,为什么要使用它们?

时间:2023-01-15 09:41:33

I just came across the concept of expression trees which I have heard multiple times. I just want to understand what is meant by an expression tree and its purpose.

我刚刚遇到了表达树的概念,我已经听过很多次了。我只是想理解表达式树的含义及其目的。

I would love it if someone could also direct me to simple explanations and samples of use.

如果有人能给我一些简单的解释和例子,我会很高兴的。

1 个解决方案

#1


64  

An Expression Tree is a data structure that contains Expressions, which is basically code. So it is a tree structure that represents a calculation you may make in code. These pieces of code can then be executed by "running" the expression tree over a set of data.

表达式树是一种包含表达式的数据结构,基本上就是代码。这是一个树形结构,表示你可以在代码中进行的计算。然后可以通过在一组数据上“运行”表达式树来执行这些代码片段。

A great thing about expression trees is that you can build them up in code; that is, you build executable code (or a sequence of steps) in code. You can also modify the code before you execute it by replacing expressions by other expressions.

表达式树的一个优点是可以用代码构建它们;也就是说,在代码中构建可执行代码(或一系列步骤)。您还可以在执行代码之前通过用其他表达式替换表达式来修改代码。

An Expression is then a function delegate, such as (int x => return x * x).

表达式是一个函数委托,例如(int x =>返回x * x)。

See also http://blogs.msdn.com/b/charlie/archive/2008/01/31/expression-tree-basics.aspx

参见http://blogs.msdn.com/b/charlie/archive/2008/01/31/expression-tree-basics.aspx

#1


64  

An Expression Tree is a data structure that contains Expressions, which is basically code. So it is a tree structure that represents a calculation you may make in code. These pieces of code can then be executed by "running" the expression tree over a set of data.

表达式树是一种包含表达式的数据结构,基本上就是代码。这是一个树形结构,表示你可以在代码中进行的计算。然后可以通过在一组数据上“运行”表达式树来执行这些代码片段。

A great thing about expression trees is that you can build them up in code; that is, you build executable code (or a sequence of steps) in code. You can also modify the code before you execute it by replacing expressions by other expressions.

表达式树的一个优点是可以用代码构建它们;也就是说,在代码中构建可执行代码(或一系列步骤)。您还可以在执行代码之前通过用其他表达式替换表达式来修改代码。

An Expression is then a function delegate, such as (int x => return x * x).

表达式是一个函数委托,例如(int x =>返回x * x)。

See also http://blogs.msdn.com/b/charlie/archive/2008/01/31/expression-tree-basics.aspx

参见http://blogs.msdn.com/b/charlie/archive/2008/01/31/expression-tree-basics.aspx