博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Clone基类--http://www.legalsoft.com.cn/docs/986.html

Posted on 2009-12-22 13:24  懒人ABC  阅读(550)  评论(0编辑  收藏  举报
  1. /// <summary>   
  2. /// BaseObject类是一个用来继承的抽象类。    
  3. /// 每一个由此类继承而来的类将自动支持克隆方法。   
  4. /// 该类实现了Icloneable接口,并且每个从该对象继承而来的对象都将同样地   
  5. /// 支持Icloneable接口。    
  6. /// </summary>    
  7. public abstract class BaseObject : ICloneable   
  8. {   
  9.     /// <summary>       
  10.     /// 克隆对象,并返回一个已克隆对象的引用       
  11.     /// </summary>       
  12.     /// <returns>引用新的克隆对象</returns>        
  13.     public object Clone()   
  14.     {   
  15.         //首先我们建立指定类型的一个实例            
  16.         object newObject = Activator.CreateInstance(this.GetType());   
  17.         //我们取得新的类型实例的字段数组。            
  18.         FieldInfo[] fields = newObject.GetType().GetFields();   
  19.         int i = 0;   
  20.         foreach (FieldInfo fi in this.GetType().GetFields())   
  21.         {   
  22.             //我们判断字段是否支持ICloneable接口。                
  23.             Type ICloneType = fi.FieldType.GetInterface("ICloneable"true);   
  24.             if (ICloneType != null)   
  25.             {   
  26.                 //取得对象的Icloneable接口。                    
  27.                 ICloneable IClone = (ICloneable)fi.GetValue(this);   
  28.                 //我们使用克隆方法给字段设定新值。                   
  29.                 fields[i].SetValue(newObject, IClone.Clone());   
  30.             }   
  31.             else  
  32.             {   
  33.                 // 如果该字段部支持Icloneable接口,直接设置即可。                    
  34.                 fields[i].SetValue(newObject, fi.GetValue(this));   
  35.             }   
  36.             //现在我们检查该对象是否支持IEnumerable接口,如果支持,                
  37.             //我们还需要枚举其所有项并检查他们是否支持IList 或 IDictionary 接口。               
  38.             Type IEnumerableType = fi.FieldType.GetInterface("IEnumerable"true);   
  39.             if (IEnumerableType != null)   
  40.             {   
  41.                 //取得该字段的IEnumerable接口                   
  42.                 IEnumerable IEnum = (IEnumerable)fi.GetValue(this);   
  43.                 Type IListType = fields[i].FieldType.GetInterface("IList"true);   
  44.                 Type IDicType = fields[i].FieldType.GetInterface("IDictionary"true);   
  45.                 int j = 0;   
  46.                 if (IListType != null)   
  47.                 {   
  48.                     //取得IList接口。                        
  49.                     IList list = (IList)fields[i].GetValue(newObject);   
  50.                     foreach (object obj in IEnum)   
  51.                     {   
  52.                         //查看当前项是否支持支持ICloneable 接口。                            
  53.                         ICloneType = obj.GetType().GetInterface("ICloneable"true);   
  54.                         if (ICloneType != null)   
  55.                         {   
  56.                             //如果支持ICloneable 接口,                
  57.                             //我们用它李设置列表中的对象的克隆              
  58.                             ICloneable clone = (ICloneable)obj;   
  59.                             list[j] = clone.Clone();   
  60.                         }   
  61.                         //注意:如果列表中的项不支持ICloneable接口,那么                         
  62.                         //在克隆列表的项将与原列表对应项相同                         
  63.                         //(只要该类型是引用类型)                           
  64.                         j++;   
  65.                     }   
  66.                 }   
  67.                 else if (IDicType != null)   
  68.                 {   
  69.                     //取得IDictionary 接口                       
  70.                     IDictionary dic = (IDictionary)fields[i].GetValue(newObject);   
  71.                     j = 0;   
  72.                     foreach (DictionaryEntry de in IEnum)   
  73.                     {   
  74.                         //查看当前项是否支持支持ICloneable 接口。                            
  75.                         ICloneType = de.Value.GetType().   
  76.                             GetInterface("ICloneable"true);   
  77.                         if (ICloneType != null)   
  78.                         {   
  79.                             ICloneable clone = (ICloneable)de.Value;   
  80.                             dic[de.Key] = clone.Clone();   
  81.                         }   
  82.                         j++;   
  83.                     }   
  84.                 }   
  85.             }   
  86.             i++;   
  87.         }   
  88.         return newObject;   
  89.     }   
  90. }