Ok, I just don't get it.
I've read about as much as I can on the subject without knowing what it's all about:
- Why use Expression Trees?
- What's a real-world example of when and how I'd use them?
- What overall benefit(s) are there in using them?
Ok, I just don't get it.
I've read about as much as I can on the subject without knowing what it's all about:
Suppose that you were writing a query provider for LINQ (for example, the SQL Server provider that translates LINQ queries to SQL) -- maybe you are a DB vendor and you want to implement support for your DB engine. You would consume the expression trees to build your sql.
They are indeed very useful.
Every lambda expression can either become a method or an expression tree.
Methods are obviously executed native in the framework.
Expression trees can be translated to other representation. For example LINQ for SQL converts query expressions into SQL syntax and execute them remotely on the server.
An expression tree is essencially a parsed bit of code handled to you as data. Your program can then interprete that data as you like at run-time. This allows you to interpret code in light of your business logic.
Say you needed to define business rules. You can define a syntax for them based on C#, and then have the new rule written directly in the source code. By accepting it into you engine as an Expression Tree, the tedious work of parsing, syntax checking and type checking is already done for you. You just have to run through the tree and apply the rule.
They are quite useful. Besides linq to sql they are used to create a strongly typed html helpers in mvc 2.0 or to provide means to use GPU for math calculation ie Brahma project.
Basically expression trees gives the ability to interpret a compiled expression instead of executing it. So they are means to create a kind of DSL in C#.
When your write something like this:
var q = from d in Data.Table
where d.Name == "SomeName"
select d
Its transformed into an expression tree. When you use this linq query agains SQL the underlaying Linq-to-SQL uses this tree to make a high performaned SQL query.
So it's used 'behind the scene' in you are doing linq stuff.
You could build your own expressions trees. This could be very useful if you want to make dynamic queries. This could be different kind of search criteria. You could even use it to make multiple inner joins with where-clauses and other cool stuff even without knowing the types your are using up-front. This though requriers some reflection to do.
Expression trees API originally was written to enable creation of custom LINQ providers. Basically, if you use LINQ to Objects you don't deal with Expression Trees. But if you work with LINQ to SQL then the LINQ provider works with Expression Trees.
In .Net 4.0 Expression Trees API was extended and is heavily used by DLR. So, if you are going to let's say add a new dynamic langauge to the .NET, you'll need it.
As it often happens, people found more ways to use expression trees. Sometimes you can use ET to get more information about objects instead of reflection. In .NET 4 you can also use them to generate dynamic methods. But remember that these are more like advanced tricks rather than recommended scenarios.
Here are some links you can take a look at:
Getting Information About Objects, Types, and Members with Expression Trees
Generating Dynamic Methods with Expression Trees in Visual Studio 2010