一个克隆对象的C#基类

一个克隆对象的C#基类

By Amir Harel

投递于 2002, 12, 30

 

摘要一个实现ICloneable接口的类。

应用于:C#, .NET

 

Download source files - 1.27 Kb

简介

虽然在现实世界中的克隆课题是有争议的, .NET世界使用它却足够安全, 难道不是吗?

为实现一个类你究竟有多少次要实现ICloneable接口, 而且每一次都写相同的代码,或为每个类写特定的代码。而且,当你的类加入一个新的字段时,往往会忘记更新这个新字段的克隆方法。如果我没说错的话,这种时候往往会带来恼人的bugs

这是我的类得以存在的原因。 藉由反射机制的小小帮助,我建立了一个用缺省行为实现了ICloneable接口的抽象类。现在或许你正在问自己: 什么是缺省行为? 那么我很高兴你这样询问。 克隆的缺省行为,是采用以下的规则来克隆类中的每一个字段:

  1. 查看一下类中的每一个字段是否支持ICloneable接口
  2. 如果某字段不支持ICloneable接口那么该字段将以常规方式处理。这意味着,如果该字段是一个值类型,那么该值被拷贝;如果该字段是一个引用类型,克隆的字段将指向同一个对象。
  3. 如果该字段支持ICloneable接口我们将使用其本身的Clone方法对其进行克隆。
  4. 如果该字段支持IEnumerable接口我们需要检查他是否支持IList IDictionary 接口如果支持,那么我们迭代该集件并且查看集合的每一项是否支持ICloneable接口。

如何使用

让你的类支持Icloneable接口所要做的就是将你的类继承自如下所述的BaseObject

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 class MyContainer()
    
    
    {
    
    
        for(int i=0 ; i<5 ; i++)
    
    
        {
    
    
             this.myArray[I] = new MyClass();
    
    
        }
    
    
    }
    
    

}

现在在Main方法中加入如下代码:

static void Main(string[] args)
    
    
{
    
    
    MyContainer con1 = new MyContainer();
    
    
    MyContainer con2 = (MyContainer)con1.Clone();
    
    
 
    
    
   con2.myArray[0].id = 5;
    
    
}
    
    

当监测con2实例时,你将会看到MyClass实例的第一项已经变为5,而con1实例却没有改变。这样你将明白加入到类中的任意支持ICloneable接口的字段将被同样地克隆。而且,如果该字段支持IList IDictionary 接口,克隆方法将侦测该字段,轮询所有项,并同样地试图对他们进行克隆

实现

/// <summary>
      
      
/// BaseObject类是一个用来继承的抽象类。

         
          
/// 每一个由此类继承而来的类将自动支持克隆方法。
/// 该类实现了Icloneable接口,并且每个从该对象继承而来的对象都将同样地
          
          
/// 支持Icloneable接口。

         
          
/// </summary>
 
    
    
public abstract class BaseObject : ICloneable
    
    
{
    
    
    /// <summary>
 /// 克隆对象,并返回一个已克隆对象的引用
    /// </summary>
      
      
    /// <returns>引用新的克隆对象</returns>

        
         
    public object Clone()
    {
    
    
       //首先我们建立指定类型的一个实例

        
         
        object newObject  = Activator.CreateInstance( this.GetType() );
 
    
    
       //我们取得新的类型实例的字段数组。

        
         
        FieldInfo[] fields = newObject.GetType().GetFields();
 
    
    
        int i = 0;
    
    
 
    
    
        foreach( FieldInfo fi in this.GetType().GetFields() )
    
    
        {
            //我们判断字段是否支持ICloneable接口。

      
       
            Type ICloneType = fi.FieldType.
                        GetInterface( "ICloneable" , true );
    
    
 
    
    
            if( ICloneType != null )
    
    
            {
    
    
              //取得对象的Icloneable接口。

        
         
                ICloneable IClone = (ICloneable)fi.GetValue(this);
 
    
    
              //我们使用克隆方法给字段设定新值。
        
        
                fields[i].SetValue( newObject , IClone.Clone() );
    
    
            }
    
    
            else
     
     
            {
    
    
              // 如果该字段部支持Icloneable接口,直接设置即可。

        
         
                fields[i].SetValue( newObject , fi.GetValue(this) );
            }
    
    
 
    
    
            //现在我们检查该对象是否支持IEnumerable接口,如果支持, 
          
      
      
            //我们还需要枚举其所有项并检查他们是否支持IList  IDictionary 接口。
            Type IEnumerableType = fi.FieldType.GetInterface
    
    
                            ( "IEnumerable" , true );
    
    
            if( IEnumerableType != null )
    
    
            {
    
    
              //取得该字段的IEnumerable接口
        
        
                IEnumerable IEnum = (IEnumerable)fi.GetValue(this);
    
    
 
    
    
              //这个版本支持IList  IDictionary 接口来迭代集合。
 
               Type IListType = fields[i].FieldType.GetInterface
     
     
                                    ( "IList" , true );
    
    
                Type IDicType = fields[i].FieldType.GetInterface
    
    
                                    ( "IDictionary" , true );
    
    
 
    
    
                int j = 0;
    
    
                if( IListType != null )
    
    
                {
		  //取得IList接口。

      
       
                    IList list = (IList)fields[i].GetValue(newObject);
 
    
    
                    foreach( object obj in IEnum )
    
    
                    {
    
    
                     //查看当前项是否支持支持ICloneable 接口。

        
         
                        ICloneType = obj.GetType().
                            GetInterface( "ICloneable" , true );
    
    
                        
    
    
                        if( ICloneType != null )
    
    
                        {
		          //如果支持ICloneable 接口
 //我们用它李设置列表中的对象的克隆
 ICloneable clone = (ICloneable)obj;
    
    
 
    
    
                            list[j] = clone.Clone();
    
    
                        }
    
    
 
    
    
                     //注意:如果列表中的项不支持ICloneable接口,那么

          
           
                     //在克隆列表的项将与原列表对应项相同

          
           
                     //(只要该类型是引用类型)
                        j++;
    
    
                    }
    
    
                }
    
    
                else if( IDicType != null )
    
    
                {
		  //取得IDictionary 接口 
       
                    IDictionary dic = (IDictionary)fields[i].
    
    
                                        GetValue(newObject);
    
    
                    j = 0;
    
    
                    
    
    
                    foreach( DictionaryEntry de in IEnum )
    
    
                    {
    
    
                     //查看当前项是否支持支持ICloneable 接口。

        
         
                        ICloneType = de.Value.GetType().
                            GetInterface( "ICloneable" , true );
    
    
 
    
    
                        if( ICloneType != null )
    
    
                        {
    
    
                            ICloneable clone = (ICloneable)de.Value;
                            dic[de.Key] = clone.Clone();
    
    
                        }
    
    
                        j++;
    
    
                    }
    
    
                }
    
    
            }
    
    
            i++;
    
    
        }
    
    
        return newObject;
    
    
    }
    
    
}
    
    

 

                                                                                    (完)

 

参看原文:Base class for cloning an object in C#

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值