Practical use of expression trees

c#, expression-trees

Solution

As Jon notes, I use them to provide generic operators with .NET 3.5. I also use them (again in MiscUtil) to provide fast access to non-default constructors (you can't use `Delegate.CreateDelegate` with constructors, but `Expression` works fine).

Other uses of manually created expression trees:

- object cloning

- dynamic LINQ sorting

- as a compiler

But really, Expression is a very versatile way of writing any dynamic code. Much simpler than `Reflection.Emit`, and for my money, simpler to understand than CodeDOM. And in .NET 4.0, you have even more options available. I show the fundamentals of writing code via `Expression` on my blog.

Problem

Expression trees are a nice feature, but what are its practical uses? Can they be used for some sort of code generation or metaprogramming or some such?

Original source

Related problems