深克隆C#代码

 看了http://www.codeproject.com/KB/cs/cloneimpl_class.aspx的文章,他的代码确实不错,稍做修改贴在这里。
href="file:///D:/temp/msohtmlclip1/01/clip_filelist.xml" rel="File-List" /> href="file:///D:/temp/msohtmlclip1/01/clip_themedata.thmx" rel="themeData" /> href="file:///D:/temp/msohtmlclip1/01/clip_colorschememapping.xml" rel="colorSchemeMapping" />

using System;

using System.Collections.Generic;

using System.Text;

using System.Reflection;

using System.Collections;

 

namespace CloneTest

{

    public class Clone

    {

    }

 

    public class MyClass : BaseObject

    {

        public string myStr ="test";

        public int id;

    }

 

    public class MyContainer : BaseObject

    {

        public string name = "test2";

        public MyClass[] myArray= new MyClass[5];

 

        public MyContainer()

        {

            for(int i=0 ; i< 5 ; i++)

            {

                 this.myArray[i] = new MyClass();

            }

        }

    }

 

    /// <summary>

 

    /// BaseObject class is an abstract class for you to derive from.

 

    /// Every class that will be dirived from this class will support the

 

    /// Clone method automaticly.

 

 

    /// The class implements the interface ICloneable and there

 

    /// for every object that will be derived

 

 

    /// from this object will support the ICloneable interface as well.

 

    /// </summary>

 

 

    public abstract class BaseObject : ICloneable

    {

        /// <summary>

 

        /// Clone the object, and returning a reference to a cloned object.

 

        /// </summary>

 

        /// <returns>Reference to the new cloned

 

        /// object.</returns>

 

        public object Clone()

        {

            //First we create an instance of this specific type.

 

            object newObject  = Activator.CreateInstance( this.GetType() );

 

            //We get the array of fields for the new type instance.

 

            FieldInfo[] fields = newObject.GetType().GetFields();

 

            int i = 0;

 

            foreach( FieldInfo fi in this.GetType().GetFields() )

            {

                //We query if the fiels support the ICloneable interface.

 

                Type ICloneType = fi.FieldType.

                            GetInterface( "ICloneable" , true );

 

                if( ICloneType != null )

                {

                    //Getting the ICloneable interface from the object.

 

                    ICloneable IClone = (ICloneable)fi.GetValue(this);

 

                    //We use the clone method to set the new value to the field.

 

                    fields[i].SetValue( newObject , IClone.Clone() );

                }

                else

                {

                    // If the field doesn't support the ICloneable

 

                    // interface then just set it.

 

                    fields[i].SetValue( newObject , fi.GetValue(this) );

                }

 

                //Now we check if the object support the

 

                //IEnumerable interface, so if it does

 

                //we need to enumerate all its items and check if

 

                //they support the ICloneable interface.

 

                Type IEnumerableType = fi.FieldType.GetInterface

                                ( "IEnumerable" , true );

                if( IEnumerableType != null )

                {

                    //Get the IEnumerable interface from the field.

 

                    IEnumerable IEnum = (IEnumerable)fi.GetValue(this);

 

                    //This version support the IList and the

 

                    //IDictionary interfaces to iterate on collections.

 

                    Type IListType = fields[i].FieldType.GetInterface

                                        ( "IList" , true );

                    Type IDicType = fields[i].FieldType.GetInterface

                                        ( "IDictionary" , true );

 

                    int j = 0;

                    if( IListType != null )

                    {

                        //Getting the IList interface.

 

                        IList list = (IList)fields[i].GetValue(newObject);

 

                        foreach( object obj in IEnum )

                        {

                            //Checking to see if the current item

 

                            //support the ICloneable interface.

 

                            ICloneType = obj.GetType().

                                GetInterface( "ICloneable" , true );

                           

                            if( ICloneType != null )

                            {

                                //If it does support the ICloneable interface,

 

                                //we use it to set the clone of

 

                                //the object in the list.

 

                                ICloneable clone = (ICloneable)obj;

 

                                list[j] = clone.Clone();

                            }

 

                            //NOTE: If the item in the list is not

 

                            //support the ICloneable interface then in the

 

                            //cloned list this item will be the same

 

                            //item as in the original list

 

                            //(as long as this type is a reference type).

 

 

                            j++;

                        }

                    }

                    else if( IDicType != null )

                    {

                        //Getting the dictionary interface.

 

                        IDictionary dic = (IDictionary)fields[i].

                                            GetValue(newObject);

                        j = 0;

                       

                        foreach( DictionaryEntry de in IEnum )

                        {

                            //Checking to see if the item

 

                            //support the ICloneable interface.

 

                            ICloneType = de.Value.GetType().

                                GetInterface( "ICloneable" , true );

 

                            if( ICloneType != null )

                            {

                                ICloneable clone = (ICloneable)de.Value;

 

                                dic[de.Key] = clone.Clone();

                            }

                            j++;

                        }

                    }

                }

                i++;

            }

            return newObject;

        }

    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值