C#對象深度克隆


有基礎的開發者都應該很明白,對象是一個引用類型,例如:

object b=new object();

object a=b;

那么a指向的是b的地址,這樣在有些時候就會造成如果修改a的值,那么b的值也會跟隨着改變(a和b是同一個引用內存地址)。

我們想要a和b都是各自互不影響的,那么只能是完全地新建一個新的對象,並且把現有對象的每個屬性的值賦給新的對象的屬性。也就是值類型的復制,這個操作就叫深度克隆。

這里我們寫兩個泛型方法分別對對象T和集合List<T>進行深度克隆的實現,我們的方法實現方式是“擴展方法”,就是能在原有的對象后面直接“點”操作。

下面我們來看一下深度克隆的算法實現:

復制代碼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;

/**
 * author:qixiao
 * create:2017-5-25 11:52:21
 * */
namespace QX_Frame.Helper_DG.Extends
{
    public static class CloneExtends
    {
        public static T DeepCloneObject<T>(this T t) where T : class
        {
            T model = System.Activator.CreateInstance<T>();                     //實例化一個T類型對象
            PropertyInfo[] propertyInfos = model.GetType().GetProperties();     //獲取T對象的所有公共屬性
            foreach (PropertyInfo propertyInfo in propertyInfos)
            {
                //判斷值是否為空,如果空賦值為null見else
                if (propertyInfo.PropertyType.IsGenericType && propertyInfo.PropertyType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
                {
                    //如果convertsionType為nullable類,聲明一個NullableConverter類,該類提供從Nullable類到基礎基元類型的轉換
                    NullableConverter nullableConverter = new NullableConverter(propertyInfo.PropertyType);
                    //將convertsionType轉換為nullable對的基礎基元類型
                    propertyInfo.SetValue(model, Convert.ChangeType(propertyInfo.GetValue(t), nullableConverter.UnderlyingType), null);
                }
                else
                {
                    propertyInfo.SetValue(model, Convert.ChangeType(propertyInfo.GetValue(t), propertyInfo.PropertyType), null);
                }
            }
            return model;
        }
        public static IList<T> DeepCloneList<T>(this IList<T> tList) where T : class
        {
            IList<T> listNew = new List<T>();
            foreach (var item in tList)
            {
                T model = System.Activator.CreateInstance<T>();                     //實例化一個T類型對象
                PropertyInfo[] propertyInfos = model.GetType().GetProperties();     //獲取T對象的所有公共屬性
                foreach (PropertyInfo propertyInfo in propertyInfos)
                {
                    //判斷值是否為空,如果空賦值為null見else
                    if (propertyInfo.PropertyType.IsGenericType && propertyInfo.PropertyType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
                    {
                        //如果convertsionType為nullable類,聲明一個NullableConverter類,該類提供從Nullable類到基礎基元類型的轉換
                        NullableConverter nullableConverter = new NullableConverter(propertyInfo.PropertyType);
                        //將convertsionType轉換為nullable對的基礎基元類型
                        propertyInfo.SetValue(model, Convert.ChangeType(propertyInfo.GetValue(item), nullableConverter.UnderlyingType), null);
                    }
                    else
                    {
                        propertyInfo.SetValue(model, Convert.ChangeType(propertyInfo.GetValue(item), propertyInfo.PropertyType), null);
                    }
                }
                listNew.Add(model);
            }
            return listNew;
        }
    }
}
復制代碼

上述代碼已經實現了深度克隆的操作,在使用上我們如下:

例如有User類,我們可以這樣操作

User user1=new User();

User user2=user1.DeepCloneObject();

這樣就完成了對user1的深度克隆!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM