开发者

Setting object properties from other object via reflection

开发者 https://www.devze.com 2023-02-03 21:29 出处:网络
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

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?


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 "


You may take a look at AutoMapper.


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消