What is the use of expression trees?

When developing applications that use the .Net language, do I not use or use the expression tree? Is it really necessary?

+4
source share
4 answers

The expression tree allows you to express your code in plain C #, but to deconstruct, validate, and interpret it. For example, you can interpret it by writing an equivalent TSQL string (example: LINQ-to-SQL or Entity Framework) or a web service request (astoria). You can interpret it as an RPC call (I wrote an RPC layer based on an expression).

Repeat blog post "Explanation of the expression" :

  • The delegate version ( Func<int,int,bool> ) is a Belgian manager; "I need you to give me a way to get from 2 integers to bool; I don't care how - when I'm ready, I will ask you - and you can tell me the answer."
  • The version of the expression ( Expr<Func<int,int,bool>> ) is an obedient analyst; "I need you to explain to me - if I gave you 2 integers, how would you go giving me a bool?"

If you have an expression, you can call Compile() to form a delegate (to do it exactly as described), or you can unlock it and do something similar based on what steps they took.


Another view of Expression is that it can act as a simplified version of ILGenerator , but still be quite universal. Very useful for metaprograms. Here's an article exploring this approach .

+4
source

Expression trees allow you to dynamically create code at run time, rather than statically injecting it into the IDE and using the compiler. They are well described in the documentation .

+1
source

Expression trees are widely used in LINQ to SQL, Entity Framework, ASP.NET MVC HTML extensions, where the runtime should interpret the expression differently (LINQ to SQL and EF: to create SQL, MVC: to define the selected property or field).

Unless you need the ability to re-interpret C # / VB expressions, you do not need to use expression trees.

(There are many things in .NET that you won’t use most of the time: for example, ServiceBase is only used when writing a service, WPF is not a web application.)

+1
source

If you do not get the benefits of expression trees, I think:
- either you do not have code that requires expression trees
- or you don’t see a place where your code can be improved using expression trees.

The first point is fine. Most of the applications I developed did not have a place for expression trees. And to deal with the second point, you just need to know what expression trees are, how they work and what they can / cannot do.

Expression trees are commonly used when you need to be able to cross some code model, for example, to create different code based on this. As already mentioned, this approach is used, for example, in Linq-to-SQL. Another example, not from the boxes, can be an application in which you need to have an object property as an object of a first class class in your application, that is, you must be able to pass the property as an instance of some class. The same can be done with reflection, but expression trees allow you to make the code less restrictive.

0
source

All Articles