表达式树有什么好处?
在使用 .Net 语言开发应用程序时,我没有得到表达式树的使用或好处?真的有必要吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
在使用 .Net 语言开发应用程序时,我没有得到表达式树的使用或好处?真的有必要吗?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
如果您没有从表达式树中受益,那么我认为:
- 要么你没有任何需要表达式树的代码
- 或者您没有看到可以通过使用表达式树来改进代码的地方。
第一点是好的,我开发的大多数应用程序都没有放置表达式树的地方。为了处理第二点,您只需要知道什么是表达式树,它们如何工作以及它们可以/不能做什么。
当您需要能够遍历某些代码模型时,通常会使用表达式树,例如为了基于此模型生成其他代码。正如已经提到的,这种方法例如用于 Linq-to-SQL 中。另一个非开箱即用的示例可能是一个应用程序,您需要将对象的属性作为应用程序中的一等公民对象,即您需要能够将该属性作为某个类的实例传递。反射也可以完成同样的事情,但表达式树允许减少此类代码的字符串类型。
If you do not get benefits of the expression trees then I think:
- either you do not have any code which requires expression trees
- or you do not see places where your code can be improved by using expression trees.
First point is Ok, most of the applications I've developed had no place for expression trees. And in order to deal with second point you just have to know what are the expression trees, how do they work and what they can/cannot do.
Expression trees are usually used when you need to have an ability to traverse some code model, for example in order to generate some other code based on this one. As already mentioned this approach is used in Linq-to-SQL for example. Another not out-of-box example might be an application where you need to have an object's property as a first class citizen object in your application, i.e. you need to be able to pass the property as an instance of some class. The same can be done with reflection but expression trees allow making such code less stringly typed.
表达式树
背后的想法是将代码放入定义良好的数据结构(即树)中,以便其他框架可以分析它、解析它并代表它运行它。:您不会在 C# 中编写
select from where
SQL 查询,但您可以用表达式树包装 C# 代码,稍后转换器会将其转换为相应的 SQL 代码。The idea behind
Expression tree
is to Put your code in well-defined data structure (i.e. tree), so other frameworks can analyze it, parse it and run it on its behalf..For Example: you would not write a
select from where
SQL-Query in C#, but you can wrap your C# code with an expression tree and later the converter will convert it to its according SQL code.表达式树允许您用常规 C# 表达您的代码,但可以对其进行解构、检查和解释。例如,您可以通过编写等效的 TSQL 字符串(例如:LINQ-to-SQL 或实体框架)或 Web 服务查询 (astoria) 来解释它。您可以将其解释为 RPC 调用(我编写了一个基于表达式的 RPC 层)。
重复一下我的“解释表达式”博客文章:
如果您有表达式,您可以调用
Compile()
来形成委托(完全按照说明进行操作),或者 你可以取消它并根据他们采取的步骤执行类似的操作。关于
Expression
的另一个观点是它可以充当ILGenerator
的简化版本 - 但是。对于元编程来说仍然非常有用。这是一篇探讨该方法的文章。An expression tree allows you to express your code in regular C#, but to deconstruct that, inspect it, and interpret it. For example, you might interpret it by writing an equivalent TSQL string (example: LINQ-to-SQL or Entity Framework), or a web-service query (astoria). You might interpret it as an RPC call (I've written an expression-based RPC layer).
To repeat my "Explaining Expression" blog entry:
If you have the expression, you can either call
Compile()
to form a delegate (to do it exactly as explained), or you can unpick it and do something similar based on what steps they took.Another view on
Expression
is that it can act as a simplified version ofILGenerator
- but still be pretty versatile. Very useful for meta-programming. Here's an article exploring that approach.表达式树允许您在运行时动态构建代码,而不是在 IDE 中静态键入代码并使用编译器。 文档中对它们进行了很好的解释。
Expression trees allow you to build code dynamically at runtime instead of statically typing it in the IDE and using a compiler. They are well explained in the documentation.
表达式树广泛用于 LINQ to SQL、实体框架、ASP.NET MVC 的 HTML 扩展,其中运行时需要以不同的方式解释表达式(LINQ to SQL 和 EF:创建 SQL、MVC:确定选定的属性或场地)。
如果您不需要重新解释 C#/VB 表达式,则无需使用表达式树。
(.NET 中有很多东西你大部分时间都不会使用:例如,ServiceBase 仅适用于编写服务,WPF 与 Web 应用程序无关。)
Expression trees are using widely in LINQ to SQL, Entity Framework, ASP.NET MVC's HTML extensions where the runtime needs to interpret the expression in a different way (LINQ to SQL and EF: to create SQL, MVC: to determine the selected property or field).
If you don't need that can of re-interpretation of a C#/VB expression you don't need to use Expression trees.
(There are plenty of things in .NET you'll not use most of the time: eg. ServiceBase only applies if writing a service, WPF is not relevant to a web app.)