zoukankan      html  css  js  c++  java
  • Clone基类http://www.legalsoft.com.cn/docs/986.html

    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. }  
  • 相关阅读:
    React antd如何实现<Upload>组件上传附件再次上传已清除附件缓存问题。
    spring项目logback日志与logstash和Elasticsearch整合
    Java后端面试经验总结分享(一)
    【设计模式】访问者模式
    【设计模式】命令模式
    【设计模式】模板方法模式
    【设计模式】代理模式
    【设计模式】享元模式
    【设计模式】外观模式
    【设计模式】组合模式
  • 原文地址:https://www.cnblogs.com/si812cn/p/1629699.html
Copyright © 2011-2022 走看看