• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
4℃空间
众人笑我痴 我笑他人看不懂
   首页    新随笔    联系   管理    订阅  订阅

返回对应对象的克隆方法

代码
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Reflection;

namespace Common.CloneObjBase
{
    
/// <summary>   
    
/// BaseObject类是一个用来继承的抽象类。    
    
/// 每一个由此类继承而来的类将自动支持克隆方法。   
    
/// 该类实现了Icloneable接口,并且每个从该对象继承而来的对象都将同样地   
    
/// 支持Icloneable接口。    
    
/// </summary>    
    public abstract class CloneObj : ICloneable
    {
        
/// <summary>
        
/// Author:Evan Lee
        
/// QQ:278631309
        
/// Blog:http://www.cnblogs.com/magic_evan
        
/// Email:Can't be Find
        
/// </summary>
        
/// <typeparam name="T">要返回的克隆对象;该对象须为class</typeparam>
        
/// <param name="Tobj">要进行克隆的对象实例</param>
        
/// <returns></returns>
        public T Clone<T>(T Tobj) where T : class
        {

            
//首先我们建立指定类型的一个实例            
            object newObject = Activator.CreateInstance(typeof(T));
            
//我们取得新的类型实例的字段数组。            
            FieldInfo[] fields = newObject.GetType().GetFields();
            
int i = 0;
            
foreach (FieldInfo fi in typeof(T).GetFields())
            {
                
//我们判断字段是否支持ICloneable接口。                
                Type ICloneType = fi.FieldType.GetInterface("ICloneable", true);
                
if (ICloneType != null)
                {
                    
//取得对象的Icloneable接口。                    
                    ICloneable IClone = (ICloneable)fi.GetValue(Tobj);
                    
//我们使用克隆方法给字段设定新值。                   
                    fields[i].SetValue(newObject, IClone.Clone());
                }
                
else
                {
                    
// 如果该字段部支持Icloneable接口,直接设置即可。                    
                    fields[i].SetValue(newObject, fi.GetValue(Tobj));
                }
                
//现在我们检查该对象是否支持IEnumerable接口,如果支持,                
                
//我们还需要枚举其所有项并检查他们是否支持IList 或 IDictionary 接口。               
                Type IEnumerableType = fi.FieldType.GetInterface("IEnumerable", true);
                
if (IEnumerableType != null)
                {
                    
//取得该字段的IEnumerable接口                   
                    IEnumerable IEnum = (IEnumerable)fi.GetValue(Tobj);
                    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 as T;
        }

        
#region ICloneable 成员

        
public object Clone()
        {
            
return base.MemberwiseClone();
            
//throw new Exception("The method or operation is not implemented.");
        }

        
#endregion
    }
}

 

posted @ 2010-04-22 23:42  magic_evan  阅读(323)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3