Click here to Skip to main content
11,481,760 members (60,679 online)
Click here to Skip to main content

Hydrating Objects With Expression Trees - Part I

, 17 Aug 2010 CPOL 11.3K 6
Rate this:
Please Sign up or sign in to vote.
Hydrating Objects With Expression Trees
free hit counters

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]);
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Paulo Morgado
Software Developer (Senior) Paulo Morgado
Portugal Portugal

Comments and Discussions

 
GeneralCool, but Pin
Omer Raviv14-Nov-10 8:37
memberOmer Raviv14-Nov-10 8:37 
GeneralRe: Cool, but Pin
Paulo Morgado14-Nov-10 11:22
memberPaulo Morgado14-Nov-10 11:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 17 Aug 2010
Article Copyright 2010 by Paulo Morgado
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid