防爆pression树傻瓜?傻瓜、pression

2023-09-02 10:42:49 作者:″痛是痛快的痛

我在这种情况下虚拟。

我试着阅读谷歌这是什么意思,但我只是不明白这一点。有人可以给我什么,他们是一个简单的解释,为什么它是有用的?

I've tried to read on Google what these are but I just don't get it. Can someone give me a simple explanation of what they are and why they're useful?

编辑:我说的是在.NET中的LINQ功能

edit: I'm talking about the LINQ feature in .Net.

推荐答案

这是EX pression树是一种机制来翻译可执行code到的数据。使用EX pression树,就可以产生重新presents你的程序的数据结构。

An expression tree is a mechanism to translate executable code into data. Using an expression tree, you can produce a data structure that represents your program.

在C#中,您可以通过使用防爆pression&LT由拉姆达EX pressions产生的前pression树工作

In C#, you can work with the expression tree produced by lambda expressions by using the Expression<T> class.

在一个传统节目,你写code是这样的:

In a traditional program, you write code like this:

double hypotenuse = Math.Sqrt(a*a + b*b);

这code使编译器生成一个任务,就是这样。在大多数情况下,这是你所关心的。

This code causes the compiler to generate an assignment, and that's it. In most cases, that's all you care about.

使用常规code,您的应用程序不能去追溯回去看看斜边来确定它是由执行制作了的Math.sqrt()通话;这些信息根本就没有什么是包含部分。

With conventional code, your application can't go retroactively back and look at hypotenuse to determine that it was produced by performing a Math.Sqrt() call; this information is simply not part of what is included.

现在,考虑一个lambda EX pression这样的:

Now, consider a lambda expression like the following:

Func<int, int, int> hypotenuse = (a, b) => Math.Sqrt(a*a + b*b);

这是比以前略有不同。现在斜边实际上是一个参考的的可执行code块的。如果你调用

This is a little different than before. Now hypotenuse is actually a reference to a block of executable code. If you call

hypotenuse(3, 4);

您将获得 5 返回的值。

我们可以使用的 EX pression树的探索中产生的可执行code块。试试这个:

We can use expression trees to explore the block of executable code that was produced. Try this instead:

Expression<Func<int, int, int>> addTwoNumbersExpression = (x, y) => x + y;
BinaryExpression body = (BinaryExpression) addTwoNumbersExpression.Body;
Console.WriteLine(body);

这将产生:

(x + y)

更先进的技术和操作都可能与前pression树木。

More advanced techniques and manipulations are possible with expression trees.