Paulo Morgado

.NET Development & Architecture

Recent Articles

view all

Events

Projects

Recent Readers

Visitor Locations

Visitor Locations

Disclaimer

The opinions and viewpoints expressed in this site are mine and do not necessarily reflect those of Microsoft, my employer or any community that I belong to. Any code or opinions are offered as is. Products or services mentioned are purchased by me, made available to me by my employer or the manufacturer/vendor which doesn't influence my opinion in any way.

Hydrating Objects With Expression Trees - Part I

LINQ With C# (Portuguese)

After my post about dumping objects using expression trees, I’ve been asked if the same could be done for hydrating objects.

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. We just have to build it by hand.

So, for a given type, the set of property setters would be built this way:

var compiledExpressions = (from property in objectType.GetProperties()
                           let objectParameterExpression = Expression.Parameter(typeof(object), "o")
                           let convertedObjectParameteExpressionr = Expression.ConvertChecked(objectParameter, objectType)
                           let valueParameter = Expression.Parameter(propertyType, "v")
                           let convertedValueParameter = Expression.ConvertChecked(valueParameter, property.PropertyType)
                           let propertyExpression = Expression.Property(convertedObjectParameter, property)
                           select
                                Expression.Lambda<Action<object, object>>(
                                    Expression.Assign(
                                        propertyExpression,
                                        convertedValueParameter
                                    ),
                                    objectParameter,
                                    valueParameter
                                ).Compile()).ToArray();

And hydrating objects would be like this:

for (int o = 0; o < objects.Length; o++)
{
    var objectProperties = objects[o];

    var newObject = newObjects[o] = Activator.CreateInstance(objectType);

    for (int p = 0; p < compiledExpressions.Length; p++)
    {
        compiledExpressions[p](newObject, objectProperties[p]);
    }
}

Comments

Twitter Trackbacks for Hydrating Objects With Expression Trees - Part I - Paulo Morgado [asp.net] on Topsy.com said:

Pingback from  Twitter Trackbacks for                 Hydrating Objects With Expression Trees - Part I - Paulo Morgado         [asp.net]        on Topsy.com

# August 16, 2010 5:55 AM

Paulo Morgado said:

Hydrating Objects With Expression Trees - Part II  

In my previous post I showed how to hydrate objects by creating instances and setting properties in those instances.

But, if the intent is to hydrate the objects from data, why not having an expression that does just that? That’s what the member initialization expression is for.

# August 17, 2010 4:02 AM

sandy said:

it was came up with rss aggregation , it is removed now

# August 17, 2010 9:32 AM

Paulo Morgado said:

@sandy,

I don't mind my blogs being aggregated in other sites, as long as its origin is mentioned.

# August 17, 2010 9:53 PM

Paulo Morgado said:

Hydrating Objects With Expression Trees - Part III

To finalize this series on object hydration, I’ll show some performance comparisons between the different methods of hydrating objects.

Code samples for this series of posts (and the one about object dumping with expression trees) can be found on my MSDN Code Gallery: Dump And Hydrate Objects With Expression Trees

# August 18, 2010 4:41 AM

Compilers: Links, News and Resources (1) « Angel \”Java\” Lopez on Blog said:

Pingback from  Compilers: Links, News and Resources (1) &laquo; Angel \&#8221;Java\&#8221; Lopez on Blog

# May 22, 2012 6:25 AM