background preloader

Expression Trees

Facebook Twitter

Untitled. Hydrating Objects With Expression Trees - Part I - Paulo Morgado. Hydrating Objects With Expression Trees - Part I After my post about dumping objects using expression trees, I’ve been asked if the same could be done for hydrating objects.

Hydrating Objects With Expression Trees - Part I - Paulo Morgado

Sure it can, but it might not be that easy. What we are looking for is a way to set properties on objects of an unknown type. For that, we need to generate methods to set each property of the objects. Such methods would look like this expression: Expression<Action<object, object>> expression = (o, v) => ((SomeType)o).Property1 = (PropertyType)v; Unfortunately, we cannot use the .NET Reflector trick because, if you try to compile this, you’ll get this error: error CS0832: An expression tree may not contain an assignment operator Fortunately, that corresponds to a valid .NET expression tree. So, for a given type, the set of property setters would be built this way: And hydrating objects would be like this:

Expression Tree Basics - Charlie Calvert's Community Blog. Newcomers to LINQ often find expression trees difficult to grasp.

Expression Tree Basics - Charlie Calvert's Community Blog

In this post I hope to show that the subject is not quite as difficult as it might appear at first. Any reader who has an intermediate level understanding of LINQ should find the material in this post easy to grasp. An expression tree provides a method of translating executable code into data. This can be very valuable if you want to modify or transform code before executing it. In particular, it can be useful if you want to transform C# code such as a LINQ query expression into code that operates on another process, such as a SQL database. Generating Dynamic Methods with Expression Trees in Visual Studio 2010 - C# Frequently Asked Questions. Expression trees first appeared in Visual Studio 2008, where they were mainly used by LINQ providers.

Generating Dynamic Methods with Expression Trees in Visual Studio 2010 - C# Frequently Asked Questions

You can use expression trees to represent code in a tree-like format, where each node is an expression. You can also convert expression trees into compiled code and run it. This transformation enables dynamic modification of executable code as well as the execution of LINQ queries in various databases and the creation of dynamic queries. Expression trees in Visual Studio 2008 are explained in Charlie Calvert’s blog post Expression Tree Basics. In this post I’m going to show how expression trees were extended in Visual Studio 2010 and how you can use them to generate dynamic methods (a problem that previously could be solved only by emitting MSIL).

Creating Expression Trees The easiest way to generate an expression tree is to create an instance of the Expression<T> type, where T is a delegate type, and assign a lambda expression to this instance. PrintExpr.Compile()(10); // Prints 10. param.