0

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?

Kevin
  • 3
  • 6
  • possible duplicate of [How to copy value from class X to class Y with the same property name in c#?](http://stackoverflow.com/questions/531505/how-to-copy-value-from-class-x-to-class-y-with-the-same-property-name-in-c) – Jon Skeet Jan 11 '11 at 17:53

4 Answers4

3

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 "

WraithNath
  • 17,658
  • 10
  • 55
  • 82
1

You may take a look at AutoMapper.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
1

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

Willem van Rumpt
  • 6,490
  • 2
  • 32
  • 44
  • Thanks - that's kinda what I was thinking, but figured I'd ask before diving into it. – Kevin Jan 11 '11 at 19:28
  • Reflection is a great asset, opening up many additional alternative routes for coding things. As with most powerful things, it's all to easy to resort to using it, before properly considering "regular" alternatives. I've bitten myself quite a few times in the past by the unlimited magic of reflection. Since then, I learned to treat it with respect ;) – Willem van Rumpt Jan 11 '11 at 21:29
0

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

yoyo
  • 8,310
  • 4
  • 56
  • 50