Kevin
Kevin

Reputation: 3

Setting object properties from other object via reflection

So basically, I've been looking for a way to clone an object and copy its properties, then add one. Seems silly to create a class, inherit the existing object then have a bunch of this.prop = obj.prop;

I thought there might be an easy way to do this with reflection and looping through the properties of obj to set the properties of 'this'. Thoughts?

Upvotes: 0

Views: 2205

Answers (4)

WraithNath
WraithNath

Reputation: 18013

Yes you can do it with reflection:

http://wraithnath.blogspot.com/2011/01/how-to-copy-and-object-with-reflection.html

                    //Copy the properties
                foreach ( PropertyInfo oPropertyInfo in oCostDept.GetType().GetProperties() )
                {
                    //Check the method is not static
                    if ( !oPropertyInfo.GetGetMethod().IsStatic )
                    {
                        //Check this property can write
                        if ( this.GetType().GetProperty( oPropertyInfo.Name ).CanWrite )
                        {
                            //Check the supplied property can read
                            if ( oPropertyInfo.CanRead )
                            {
                                //Update the properties on this object
                                this.GetType().GetProperty( oPropertyInfo.Name ).SetValue( this, oPropertyInfo.GetValue( oCostDept, null ), null );
                            }
                        }
                    }
                }

[1]: http://wraithnath.blogspot.com/2011/01/how-to-copy-and-object-with-reflection.html "

Upvotes: 3

yoyo
yoyo

Reputation: 8708

If the classes are serializable you could probably use Xml serialization to serialize one class, then deserialize to the other.

Upvotes: 0

Willem van Rumpt
Willem van Rumpt

Reputation: 6570

It all depends on the kind of cloning you want. If you want shallow cloning, it's easy enough. Looping through the properties of the object and setting them on the clone will do just that. But that means that properties containing references will reference the same object for both the source as the clone.

If you want a deep clone, you'll have to find a way to also clone references owned by the source object (and references owned by the references owned by the source, etc. etc.). This may not be possible to do in automated way, if those references don't have default constructors.

What it boils down to is that, in my experience, if you have a non-trivial class and/or class hierarchy (notably with the possibility of not-existing default constructors), the easiest, most reliable way, is to just write either a "copy constructor" (which doesn't exists as such in .NET), and do the work yourself, implement ICloneable, and do the work yourself, or implement your own kind of Clone method, and do the work yourself ;)

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

You may take a look at AutoMapper.

Upvotes: 1

Related Questions