程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> [C#進階系列]專題一:深入解析深拷貝和淺拷貝,

[C#進階系列]專題一:深入解析深拷貝和淺拷貝,

編輯:C#入門知識

[C#進階系列]專題一:深入解析深拷貝和淺拷貝,


一、前言

  這個星期參加了一個面試,面試中問到深淺拷貝的區別,然後我就簡單了講述了它們的之間的區別,然後面試官又繼續問,如何實現一個深拷貝呢?當時只回答回答了一種方式,就是使用反射,然後面試官提示還可以通過反序列化和表達樹的方式。然後又繼續問,如果用反射來實現深拷貝的話,如何解決互相引用對象的問題呢? 當時我給出的答案是說那就不用反射去實現呗,用反序列化實現呗,或者直接避免使兩個對象互相引用呗。然後面試官說,如果一定用反射來寫,你是怎麼去解決這個問題呢?這時候我就愣住了。

  這樣也就有了這篇文章。今天就來深入解析下深淺拷貝的問題。

二、深拷貝 Vs 淺拷貝

  首先,講到深淺拷貝,自然就有一個問題來了?什麼是深拷貝,什麼又是淺拷貝呢?下面就具體介紹下它們的定義。

  深拷貝:指的是拷貝一個對象時,不僅僅把對象的引用進行復制,還把該對象引用的值也一起拷貝。這樣進行深拷貝後的拷貝對象就和源對象互相獨立,其中任何一個對象的改動都不會對另外一個對象造成影響。舉個例子,一個人叫張三,然後使用克隆技術以張三來克隆另外一個人叫李四,這樣張三和李四就是相互獨立的,不管張三缺胳膊還是李四少腿了都不會影響另外一個人。在.NET領域,值對象就是典型的例子,如int, Double以及結構體和枚舉等。具體例子如下所示:

int source = 123;
// 值類型賦值內部執行深拷貝
int copy = source;
// 對拷貝對象進行賦值不會改變源對象的值
copy = 234;
// 同樣對源對象賦值也不會改變拷貝對象的值
source = 345;

  淺拷貝:指的是拷貝一個對象時,僅僅拷貝對象的引用進行拷貝,但是拷貝對象和源對象還是引用同一份實體。此時,其中一個對象的改變都會影響到另一個對象。例如,一個人一開始叫張三,後來改名字為張老三了,可是他們還是同一個人,不管張三缺胳膊還是張老三少腿,都反應在同一個人身上。在.NET中引用類型就是一個例子。如類類型。具體例子如下所示:

public class Person
    {
        public string Name { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Person sourceP = new Person() { Name = "張三" };
            Person copyP = sourceP; // 淺拷貝
            copyP.Name = "張老三"; // 拷貝對象改變Name值
            // 結果都是"張老三",因為實現的是淺拷貝,一個對象的改變都會影響到另一個對象
            Console.WriteLine("Person.Name: [SourceP: {0}] [CopyP:{1}]", sourceP.Name, copyP.Name);
            Console.Read();
        }
    }

三、深淺拷貝的幾種實現方式

   上面已經明白了深淺拷貝的定義,至於他們之間的區別也在定義中也有所體現。介紹完了它們的定義和區別之後,自然也就有了如何去實現它們呢?

  對於,淺拷貝的實現方式很簡單,.NET自身也提供了實現。我們知道,所有對象的父對象都是System.Object對象,這個父對象中有一個MemberwiseClone方法,該方法就可以用來實現淺拷貝,下面具體看看淺拷貝的實現方式,具體演示代碼如下所示:

// 繼承ICloneable接口,重新其Clone方法
    class ShallowCopyDemoClass : ICloneable
    {
        public int intValue = 1;
        public string strValue = "1";
        public PersonEnum pEnum = PersonEnum.EnumA;
        public PersonStruct pStruct = new PersonStruct() {  StructValue = 1};
        public Person pClass = new Person("1");
        public int[] pIntArray = new int[] { 1 };
        public string[] pStringArray = new string[] { "1" };

        #region ICloneable成員
        public object Clone()
        {
            return this.MemberwiseClone();
        }

        #endregion 

    }

    class Person
    {
        public string Name;
        public Person(string name)
        {
            Name = name;
        }
    }

    public enum PersonEnum
    {
        EnumA = 0,
        EnumB = 1
    }

    public struct PersonStruct
    {
        public int StructValue;
    }

  上面類中重寫了IConeable接口的Clone方法,其實現直接調用了Object的MemberwiseClone方法來完成淺拷貝,如果想實現深拷貝,也可以在Clone方法中實現深拷貝的邏輯。接下來就是對上面定義的類進行淺拷貝測試了,看看是否是實現的淺拷貝,具體演示代碼如下所示:

class Program
    {
        static void Main(string[] args)
        {
            ShallowCopyDemo();
            // List淺拷貝的演示
            ListShallowCopyDemo();
        }

        public static void ListShallowCopyDemo()
        {
            List<PersonA> personList = new List<PersonA>() 
            {
                new PersonA() { Name="PersonA", Age= 10, ClassA= new A() { TestProperty = "AProperty"} },
                new PersonA() { Name="PersonA2", Age= 20, ClassA= new A() { TestProperty = "AProperty2"} }
            };
            // 下面2種方式實現的都是淺拷貝
            List<PersonA> personsCopy = new List<PersonA>(personList);
            PersonA[] personCopy2 = new PersonA[2];
            personList.CopyTo(personCopy2);

       // 由於實現的是淺拷貝,所以改變一個對象的值,其他2個對象的值都會發生改變,因為它們都是使用的同一份實體,即它們指向內存中同一個地址  personsCopy.First().ClassA.TestProperty = "AProperty3"; WriteLog(string.Format("personCopy2.First().ClassA.TestProperty is {0}", personCopy2.First().ClassA.TestProperty)); WriteLog(string.Format("personList.First().ClassA.TestProperty is {0}", personList.First().ClassA.TestProperty)); WriteLog(string.Format("personsCopy.First().ClassA.TestProperty is {0}", personsCopy.First().ClassA.TestProperty));
       Console.Read();  } public static void ShallowCopyDemo() { ShallowCopyDemoClass DemoA = new ShallowCopyDemoClass(); ShallowCopyDemoClass DemoB = DemoA.Clone() as ShallowCopyDemoClass ; DemoB.intValue = 2; WriteLog(string.Format(" int->[A:{0}] [B:{1}]", DemoA.intValue, DemoB.intValue)); DemoB.strValue = "2"; WriteLog(string.Format(" string->[A:{0}] [B:{1}]", DemoA.strValue, DemoB.strValue)); DemoB.pEnum = PersonEnum.EnumB; WriteLog(string.Format(" Enum->[A: {0}] [B:{1}]", DemoA.pEnum, DemoB.pEnum)); DemoB.pStruct.StructValue = 2; WriteLog(string.Format(" struct->[A: {0}] [B: {1}]", DemoA.pStruct.StructValue, DemoB.pStruct.StructValue)); DemoB.pIntArray[0] = 2; WriteLog(string.Format(" intArray->[A:{0}] [B:{1}]", DemoA.pIntArray[0], DemoB.pIntArray[0])); DemoB.pStringArray[0] = "2"; WriteLog(string.Format("stringArray->[A:{0}] [B:{1}]", DemoA.pStringArray[0], DemoB.pStringArray[0])); DemoB.pClass.Name = "2"; WriteLog(string.Format(" Class->[A:{0}] [B:{1}]", DemoA.pClass.Name, DemoB.pClass.Name));
       Console.WriteLine();
      }
private static void WriteLog(string msg) { Console.WriteLine(msg); }   } }

  上面代碼的運行結果如下圖所示:

  從上面運行結果可以看出,.NET中值類型默認是深拷貝的,而對於引用類型,默認實現的是淺拷貝。所以對於類中引用類型的屬性改變時,其另一個對象也會發生改變。

  上面已經介紹了淺拷貝的實現方式,那深拷貝要如何實現呢?在前言部分已經介紹了,實現深拷貝的方式有:反射、反序列化和表達式樹。在這裡,我只介紹反射和反序列化的方式,對於表達式樹的方式在網上也沒有找到,當時面試官說是可以的,如果大家找到了表達式樹的實現方式,麻煩還請留言告知下。下面我們首先來看看反射的實現方式吧:

// 利用反射實現深拷貝
        public static T DeepCopyWithReflection<T>(T obj)
        {
            Type type = obj.GetType();

            // 如果是字符串或值類型則直接返回
            if (obj is string || type.IsValueType) return obj;

            if (type.IsArray)
            {
                Type elementType = Type.GetType(type.FullName.Replace("[]", string.Empty));
                var array = obj as Array;
                Array copied = Array.CreateInstance(elementType, array.Length);
                for (int i = 0; i < array.Length; i++)
                {
                    copied.SetValue(DeepCopyWithReflection(array.GetValue(i)), i);
                }

                return (T)Convert.ChangeType(copied, obj.GetType());
            }

            object retval = Activator.CreateInstance(obj.GetType());
            
            PropertyInfo[] properties = obj.GetType().GetProperties(
                BindingFlags.Public | BindingFlags.NonPublic
                | BindingFlags.Instance | BindingFlags.Static);
            foreach (var property in properties)
            {
                var propertyValue = property.GetValue(obj, null);
                if (propertyValue == null)
                    continue;
                property.SetValue(retval, DeepCopyWithReflection(propertyValue), null);
            }

            return (T)retval;
        }

  反序列化的實現方式,反序列化的方式也可以細分為3種,具體的實現如下所示:

 // 利用XML序列化和反序列化實現
        public static T DeepCopyWithXmlSerializer<T>(T obj)
        {
            object retval;
            using (MemoryStream ms = new MemoryStream())
            {
                XmlSerializer xml = new XmlSerializer(typeof(T));
                xml.Serialize(ms, obj);
                ms.Seek(0, SeekOrigin.Begin);
                retval = xml.Deserialize(ms);
                ms.Close();
            }

            return (T)retval;
        }

        // 利用二進制序列化和反序列實現
        public static T DeepCopyWithBinarySerialize<T>(T obj)
        {
            object retval;
            using (MemoryStream ms = new MemoryStream())
            {
                BinaryFormatter bf = new BinaryFormatter();
                // 序列化成流
                bf.Serialize(ms, obj);
                ms.Seek(0, SeekOrigin.Begin);
                // 反序列化成對象
                retval = bf.Deserialize(ms);
                ms.Close();
            }

            return (T)retval;
        }

        // 利用DataContractSerializer序列化和反序列化實現
        public static T DeepCopy<T>(T obj)
        {
            object retval;
            using (MemoryStream ms = new MemoryStream())
            {
                DataContractSerializer ser = new DataContractSerializer(typeof(T));
                ser.WriteObject(ms, obj);
                ms.Seek(0, SeekOrigin.Begin);
                retval = ser.ReadObject(ms);
                ms.Close();
            }
            return (T)retval;
        }
        
        // 表達式樹實現
        // ....

四、使用反射進行深拷貝如何解決相互引用的問題

  上面反射的實現方式,對於相互引用的對象會出現StackOverflower的錯誤,由於對象的相互引用,會導致方法循環調用。下面就是一個相互引用對象的例子:

[Serializable]
    public class DeepCopyDemoClass
    {
        public string Name {get;set;}
        public int[] pIntArray { get; set; }
        public Address Address { get; set; }
        public DemoEnum DemoEnum { get; set; }

        // DeepCopyDemoClass中引用了TestB對象,TestB類又引用了DeepCopyDemoClass對象,從而造成了相互引用
        public TestB TestB {get;set;}

        public override string ToString()
        {
            return "DeepCopyDemoClass";
        }
    }

    [Serializable]
    public class TestB
    {
        public string Property1 { get; set; }

        public DeepCopyDemoClass DeepCopyClass { get; set; }

        public override string ToString()
        {
            return "TestB Class";
        }
    }

    [Serializable]
    public struct Address
    {
        public string City { get; set; }
    }

    public enum DemoEnum
    {
        EnumA = 0,
        EnumB = 1
    }

  在面試過程中,針對這個問題的解決方式我回答的是不知道,回來之後思考了之後,也就有了點思路。首先想到的是:能不能用一個字典來記錄每個對象被反射的次數,仔細想想可行,於是開始實現,初步修復後的反射實現如下所示:

  public class DeepCopyHelper
    {
        // 用一個字典來存放每個對象的反射次數來避免反射代碼的循環遞歸
        static Dictionary<Type, int> typereflectionCountDic = new Dictionary<Type, int>();

 public static T DeepCopyWithReflection_Second<T>(T obj)
        {
            Type type = obj.GetType();

            // 如果是字符串或值類型則直接返回
            if (obj is string || type.IsValueType) return obj;

            if (type.IsArray)
            {
                Type elementType = Type.GetType(type.FullName.Replace("[]", string.Empty));
                var array = obj as Array;
                Array copied = Array.CreateInstance(elementType, array.Length);
                for (int i = 0; i < array.Length; i++)
                {
                    copied.SetValue(DeepCopyWithReflection_Second(array.GetValue(i)), i);
                }

                return (T)Convert.ChangeType(copied, obj.GetType());
            }

            // 對於類類型開始記錄對象反射的次數
            int reflectionCount = Add(typereflectionCountDic, obj.GetType());
            if (reflectionCount > 1)
                return obj; // 這裡有錯誤

            object retval = Activator.CreateInstance(obj.GetType());

            PropertyInfo[] properties = obj.GetType().GetProperties(
                BindingFlags.Public | BindingFlags.NonPublic
                | BindingFlags.Instance | BindingFlags.Static);
            foreach (var property in properties)
            {
                var propertyValue = property.GetValue(obj, null);
                if (propertyValue == null)
                    continue;
                property.SetValue(retval, DeepCopyWithReflection_Second(propertyValue), null);
            }

            return (T)retval;
        }
        private static int Add(Dictionary<Type, int> dict, Type key)
        {
            if (key.Equals(typeof(String)) || key.IsValueType) return 0;
            if (!dict.ContainsKey(key))
            {
                dict.Add(key, 1);
                return dict[key];
            }

            dict[key] += 1;
            return dict[key];
        }
}    

  下面用代碼來測試下上面的代碼是否已經解決了循環遞歸的問題,具體的測試代碼如下所示:

class Program
    {
        static void Main(string[] args)
        {
            //ShallowCopyDemo();
            //ListShallowCopyDemo();
            DeepCopyDemo();
            DeepCopyDemo2();
        }
        private static void WriteLog(string msg)
        {
            Console.WriteLine(msg);
        }

        public static void DeepCopyDemo()
        {
            DeepCopyDemoClass deepCopyClassA = new DeepCopyDemoClass();
            deepCopyClassA.Name = "DeepCopyClassDemo";
            deepCopyClassA.pIntArray = new int[] { 1 };
            deepCopyClassA.DemoEnum = DemoEnum.EnumA;
            deepCopyClassA.Address = new Address() { City = "Shanghai" };

            deepCopyClassA.TestB = new TestB() { Property1 = "TestProperty", DeepCopyClass = deepCopyClassA };

            // 使用反序列化來實現深拷貝
            DeepCopyDemoClass deepCopyClassB = DeepCopyHelper.DeepCopyWithBinarySerialize<DeepCopyDemoClass>(deepCopyClassA);
            deepCopyClassB.Name = "DeepCopyClassDemoB";
            WriteLog(string.Format("    Name->[A:{0}] [B:{1}]", deepCopyClassA.Name, deepCopyClassB.Name));
            deepCopyClassB.pIntArray[0] = 2;
            WriteLog(string.Format("    intArray->[A:{0}] [B:{1}]", deepCopyClassA.pIntArray[0], deepCopyClassB.pIntArray[0]));
            deepCopyClassB.Address = new Address() { City = "Beijing" };
            WriteLog(string.Format("    Addressstruct->[A: {0}] [B: {1}]", deepCopyClassA.Address.City, deepCopyClassB.Address.City));
            deepCopyClassB.DemoEnum = DemoEnum.EnumB;
            WriteLog(string.Format("    DemoEnum->[A: {0}] [B: {1}]", deepCopyClassA.DemoEnum, deepCopyClassB.DemoEnum));
            deepCopyClassB.TestB.Property1 = "TestPropertyB";
            WriteLog(string.Format("    Property1->[A:{0}] [B:{1}]", deepCopyClassA.TestB.Property1, deepCopyClassB.TestB.Property1));
            WriteLog(string.Format("    TestB.DeepCopyClass.Name->[A:{0}] [B:{1}]", deepCopyClassA.TestB.DeepCopyClass.Name, deepCopyClassB.TestB.DeepCopyClass.Name));
            Console.WriteLine();
        }

        public static void DeepCopyDemo2()
        {
            DeepCopyDemoClass deepCopyClassA = new DeepCopyDemoClass();
            deepCopyClassA.Name = "DeepCopyClassDemo";
            deepCopyClassA.pIntArray = new int[] { 1, 2 };
            deepCopyClassA.DemoEnum = DemoEnum.EnumA;
            deepCopyClassA.Address = new Address() { City = "Shanghai" };

            deepCopyClassA.TestB = new TestB() { Property1 = "TestProperty",  DeepCopyClass = deepCopyClassA };

            // 使用反射來完成深拷貝
            DeepCopyDemoClass deepCopyClassB = DeepCopyHelper.DeepCopyWithReflection_Second<DeepCopyDemoClass>(deepCopyClassA);

            //DeepCopyDemoClass deepCopyClassB = DeepCopyHelper.DeepCopyWithReflection<DeepCopyDemoClass>(deepCopyClassA);
            deepCopyClassB.Name = "DeepCopyClassDemoB";
            WriteLog(string.Format("    Name->[A:{0}] [B:{1}]", deepCopyClassA.Name, deepCopyClassB.Name));
            deepCopyClassB.pIntArray[0] = 2;
            WriteLog(string.Format("    intArray->[A:{0}] [B:{1}]", deepCopyClassA.pIntArray[0], deepCopyClassB.pIntArray[0]));
            deepCopyClassB.Address = new Address() { City = "Beijing" };
            WriteLog(string.Format("    Addressstruct->[A: {0}] [B: {1}]", deepCopyClassA.Address.City, deepCopyClassB.Address.City));
            deepCopyClassB.DemoEnum = DemoEnum.EnumB;
            WriteLog(string.Format("    DemoEnum->[A: {0}] [B: {1}]", deepCopyClassA.DemoEnum, deepCopyClassB.DemoEnum));
            deepCopyClassB.TestB.Property1 = "TestPropertyB";
            WriteLog(string.Format("    Property1->[A:{0}] [B:{1}]", deepCopyClassA.TestB.Property1, deepCopyClassB.TestB.Property1));
            WriteLog(string.Format("    TestB.DeepCopyClass.Name->[A:{0}] [B:{1}]", deepCopyClassA.TestB.DeepCopyClass.Name, deepCopyClassB.TestB.DeepCopyClass.Name));
            Console.ReadKey();
        }
    }    

  此時的運行結果如下圖所示:

  剛開始看到這樣的運行結果,開心地以為已經解決了循環遞歸的問題了,因為此時結果成功運行出來了,沒有了StackOverflower的錯誤了。但是仔細一看,反序列化和反射完成的深拷貝的運行結果不一樣,如上圖中紅色圈出來的部分。顯然,反序列化的結果是沒有錯誤的,顯然目前實現的反射代碼還是有問題的。接下來就是思考了。為什麼上面反射的代碼不正確呢?

  仔細分析DeepCopyWithReflection_Second中的代碼,發現下面代碼紅色部分是錯誤的:

int reflectionCount = Add(typereflectionCountDic, obj.GetType());
            if (reflectionCount > 1)
                return obj; // 是錯誤的

  對DeepCopyWithReflection_Second方法仔細分析,在對TestB進行反射時,當反射到DeepCopyClass屬性時,此時會遞歸調用DeepCopyWithReflection_Second方法,此時在typereflectionCountDic發現DeepCopyDemoClass已經被反射了,則直接返回,這樣分析好像沒什麼錯誤,但是此時返回的是deepCopyClassA對象,但是我們需要返回的是deepCopyClassB對象,即此時deepCopyClassB對象的內存結構如下圖所示:

public static T DeepCopyWithReflection_Third<T>(T obj) { Type type = obj.GetType(); // 如果是字符串或值類型則直接返回 if (obj is string || type.IsValueType) return obj; if (type.IsArray) { Type elementType = Type.GetType(type.FullName.Replace("[]", string.Empty)); var array = obj as Array; Array copied = Array.CreateInstance(elementType, array.Length); for (int i = 0; i < array.Length; i++) { copied.SetValue(DeepCopyWithReflection_Second(array.GetValue(i)), i); } return (T)Convert.ChangeType(copied, obj.GetType()); } int reflectionCount = Add(typereflectionCountDic, obj.GetType()); if (reflectionCount > 1 && obj.GetType() == typeof(DeepCopyDemoClass)) return (T)DeepCopyDemoClasstypeRef; // 返回deepCopyClassB對象 object retval = Activator.CreateInstance(obj.GetType()); if(retval.GetType() == typeof(DeepCopyDemoClass)) DeepCopyDemoClasstypeRef = retval; // 保存一開始創建的DeepCopyDemoClass對象 PropertyInfo[] properties = obj.GetType().GetProperties( BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static); foreach (var property in properties) { var propertyValue = property.GetValue(obj, null); if (propertyValue == null) continue; property.SetValue(retval, DeepCopyWithReflection_Third(propertyValue), null); } return (T)retval; }

  下面我用DeepCopyWithReflection_Third方法來測試下,具體的測試代碼如下所示:

 class Program
    {
        static void Main(string[] args)
        {
            //ShallowCopyDemo();
            //ListShallowCopyDemo();
            DeepCopyDemo();
            DeepCopyDemo2();
        }
         private static void WriteLog(string msg)
        {
            Console.WriteLine(msg);
        }

        public static void DeepCopyDemo()
        {
            DeepCopyDemoClass deepCopyClassA = new DeepCopyDemoClass();
            deepCopyClassA.Name = "DeepCopyClassDemo";
            deepCopyClassA.pIntArray = new int[] { 1 };
            deepCopyClassA.DemoEnum = DemoEnum.EnumA;
            deepCopyClassA.Address = new Address() { City = "Shanghai" };

            deepCopyClassA.TestB = new TestB() { Property1 = "TestProperty", DeepCopyClass = deepCopyClassA };

            // 使用反序列化來實現深拷貝
            DeepCopyDemoClass deepCopyClassB = DeepCopyHelper.DeepCopyWithBinarySerialize<DeepCopyDemoClass>(deepCopyClassA);
            deepCopyClassB.Name = "DeepCopyClassDemoB";
            WriteLog(string.Format("    Name->[A:{0}] [B:{1}]", deepCopyClassA.Name, deepCopyClassB.Name));
            deepCopyClassB.pIntArray[0] = 2;
            WriteLog(string.Format("    intArray->[A:{0}] [B:{1}]", deepCopyClassA.pIntArray[0], deepCopyClassB.pIntArray[0]));
            deepCopyClassB.Address = new Address() { City = "Beijing" };
            WriteLog(string.Format("    Addressstruct->[A: {0}] [B: {1}]", deepCopyClassA.Address.City, deepCopyClassB.Address.City));
            deepCopyClassB.DemoEnum = DemoEnum.EnumB;
            WriteLog(string.Format("    DemoEnum->[A: {0}] [B: {1}]", deepCopyClassA.DemoEnum, deepCopyClassB.DemoEnum));
            deepCopyClassB.TestB.Property1 = "TestPropertyB";
            WriteLog(string.Format("    Property1->[A:{0}] [B:{1}]", deepCopyClassA.TestB.Property1, deepCopyClassB.TestB.Property1));
            WriteLog(string.Format("    TestB.DeepCopyClass.Name->[A:{0}] [B:{1}]", deepCopyClassA.TestB.DeepCopyClass.Name, deepCopyClassB.TestB.DeepCopyClass.Name));
            Console.WriteLine();
        }

        public static void DeepCopyDemo2()
        {
            DeepCopyDemoClass deepCopyClassA = new DeepCopyDemoClass();
            deepCopyClassA.Name = "DeepCopyClassDemo";
            deepCopyClassA.pIntArray = new int[] { 1, 2 };
            deepCopyClassA.DemoEnum = DemoEnum.EnumA;
            deepCopyClassA.Address = new Address() { City = "Shanghai" };

            deepCopyClassA.TestB = new TestB() { Property1 = "TestProperty",  DeepCopyClass = deepCopyClassA };

            // 使用反射來完成深拷貝
            DeepCopyDemoClass deepCopyClassB = DeepCopyHelper.DeepCopyWithReflection_Third<DeepCopyDemoClass>(deepCopyClassA);

            //DeepCopyDemoClass deepCopyClassB = DeepCopyHelper.DeepCopyWithReflection<DeepCopyDemoClass>(deepCopyClassA);
            deepCopyClassB.Name = "DeepCopyClassDemoB";
            WriteLog(string.Format("    Name->[A:{0}] [B:{1}]", deepCopyClassA.Name, deepCopyClassB.Name));
            deepCopyClassB.pIntArray[0] = 2;
            WriteLog(string.Format("    intArray->[A:{0}] [B:{1}]", deepCopyClassA.pIntArray[0], deepCopyClassB.pIntArray[0]));
            deepCopyClassB.Address = new Address() { City = "Beijing" };
            WriteLog(string.Format("    Addressstruct->[A: {0}] [B: {1}]", deepCopyClassA.Address.City, deepCopyClassB.Address.City));
            deepCopyClassB.DemoEnum = DemoEnum.EnumB;
            WriteLog(string.Format("    DemoEnum->[A: {0}] [B: {1}]", deepCopyClassA.DemoEnum, deepCopyClassB.DemoEnum));
            deepCopyClassB.TestB.Property1 = "TestPropertyB";
            WriteLog(string.Format("    Property1->[A:{0}] [B:{1}]", deepCopyClassA.TestB.Property1, deepCopyClassB.TestB.Property1));
            WriteLog(string.Format("    TestB.DeepCopyClass.Name->[A:{0}] [B:{1}]", deepCopyClassA.TestB.DeepCopyClass.Name, deepCopyClassB.TestB.DeepCopyClass.Name));
            Console.ReadKey();
        }
}

  此時的運行結果如下圖示所示:

  

  從上面的測試結果可以看出,此時深拷貝的反射實現方法基本上沒什麼問題了。這個方法也同時解決了相互引用對象的循環遞歸問題。

五、總結

  到這裡,該文章的內容就結束。這裡主要記錄下自己在一次面試過程中遇到問題的一次總結,從中可以看出,反射進行深拷貝會有很多其他的問題,所以平時還是建議大家使用序列化的形式來進行深拷貝。

  最後附上本文所有源碼下載:DeepCopy.zip

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved