Whats a good use case for .net 4.0 Expression Trees?

.net expression-trees linq

Question

This one was inspired by my language-guru co-worker who can't seem to find a good use for them, and after a few lame attempts of my own, I'd have to agree.

Now I know these concepts tend to flow a lot more easily once you get some good practical reasons down.

At the moment it seems as though its only purpose is to allow you to write a Linq provider?

Is that it?? Is there any other benefits to this?

Accepted Answer

Expression tree are so powerful because they let you treat code like data. Users are accustomed to building up data, saving it and coming back to it later.

Expression trees let you do the same thing with code. For example you can take your user's input (check-boxes, number ranges, etc.) and translate it into an Expression tree. That expression tree can then be executed, or stored off for later use. Very cool.

Think of the practical uses around reporting like building up and saving data filters and data mappings. Another practical use would be to support custom work flows in your application based on user defined rules.

Here's a bit of MSDN code on serializing expression trees (http://code.msdn.microsoft.com/exprserialization) that should get the ideas flowing.


Popular Answer

You can use Expression Trees to transform a domain language into executable code.




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why