Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expression trees in .NET - Libraries? [closed]

I have recently written a dynamic querying tool using expression trees and as I went the power of expression trees began to dawn on me. My querying tool could also form the basis of a reporting tool, a dynamic decision engine and maybe other cases where you need to work with dynamic objects in abstract ways.

The process has been painful (recursive calls to generic methods by reflection - ugh) so I am wondering whether anyone has created a generic library that might harness the power of expression trees to accomplish some of these kinds of tasks? I have seen PredicateBuilder but does anyone know of any others?

like image 564
flesh Avatar asked Dec 04 '08 00:12

flesh


People also ask

What .NET feature do you use to create an expression tree?

In . NET Framework 4 or later, the expression trees API also supports assignments and control flow expressions such as loops, conditional blocks, and try-catch blocks. By using the API, you can create expression trees that are more complex than those that can be created from lambda expressions by the C# compiler.

What is expression tree in C#?

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. In C#, you can work with the expression tree produced by lambda expressions by using the Expression<T> class.

Why do we need expression tree?

Expression trees are often used to generate code dynamically at runtime. Lets say that you have to create lots of unknown type insatnces. You could create them using reflection and you'll suffer from poor performance, or you can create an expression tree and compile it to a method.

How do you run expression tree?

Expression trees that represent lambda expressions are of type LambdaExpression or Expression<TDelegate>. To execute these expression trees, call the Compile method to create an executable delegate, and then invoke the delegate.


2 Answers

Generic operators (in MiscUtil) is a pretty well utilised bit of code based on expression trees; very simple, but very versatile. Some other ideas:

  • cloning objects
  • dynamic ordering
  • dynamic LINQ

(lots of others)

like image 114
Marc Gravell Avatar answered Nov 05 '22 15:11

Marc Gravell


Have you looked at the DLR? I don't know much about it in detail, but my understanding is that the expression-tree mechanism which is behind the scenes in Linq-to-SQL has effectively been published in the DLR.

like image 1
Will Dean Avatar answered Nov 05 '22 15:11

Will Dean