您好,登錄后才能下訂單哦!
這個星期參加了一個面試,面試中問到深淺拷貝的區(qū)別,然后我就簡單了講述了它們的之間的區(qū)別,然后面試官又繼續(xù)問,如何實現(xiàn)一個深拷貝呢?當時只回答回答了一種方式,就是使用反射,然后面試官提示還可以通過反序列化和表達樹的方式。然后又繼續(xù)問,如果用反射來實現(xiàn)深拷貝的話,如何解決互相引用對象的問題呢? 當時我給出的答案是說那就不用反射去實現(xiàn)唄,用反序列化實現(xiàn)唄,或者直接避免使兩個對象互相引用唄。然后面試官說,如果一定用反射來寫,你是怎么去解決這個問題呢?這時候我就愣住了。
這樣也就有了這篇文章。今天就來深入解析下深淺拷貝的問題。
首先,講到深淺拷貝,自然就有一個問題來了?什么是深拷貝,什么又是淺拷貝呢?下面就具體介紹下它們的定義。
深拷貝:指的是拷貝一個對象時,不僅僅把對象的引用進行復制,還把該對象引用的值也一起拷貝。這樣進行深拷貝后的拷貝對象就和源對象互相獨立,其中任何一個對象的改動都不會對另外一個對象造成影響。舉個例子,一個人叫張三,然后使用克隆技術(shù)以張三來克隆另外一個人叫李四,這樣張三和李四就是相互獨立的,不管張三缺胳膊還是李四少腿了都不會影響另外一個人。在.NET領(lǐng)域,值對象就是典型的例子,如int, Double以及結(jié)構(gòu)體和枚舉等。具體例子如下所示:
int source = 123; // 值類型賦值內(nèi)部執(zhí)行深拷貝 int copy = source; // 對拷貝對象進行賦值不會改變源對象的值 copy = 234; // 同樣對源對象賦值也不會改變拷貝對象的值 source = 345;
淺拷貝:指的是拷貝一個對象時,僅僅拷貝對象的引用進行拷貝,但是拷貝對象和源對象還是引用同一份實體。此時,其中一個對象的改變都會影響到另一個對象。例如,一個人一開始叫張三,后來改名字為張老三了,可是他們還是同一個人,不管張三缺胳膊還是張老三少腿,都反應(yīng)在同一個人身上。在.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值 // 結(jié)果都是"張老三",因為實現(xiàn)的是淺拷貝,一個對象的改變都會影響到另一個對象 Console.WriteLine("Person.Name: [SourceP: {0}] [CopyP:{1}]", sourceP.Name, copyP.Name); Console.Read(); } }
上面已經(jīng)明白了深淺拷貝的定義,至于他們之間的區(qū)別也在定義中也有所體現(xiàn)。介紹完了它們的定義和區(qū)別之后,自然也就有了如何去實現(xiàn)它們呢?
對于,淺拷貝的實現(xiàn)方式很簡單,.NET自身也提供了實現(xiàn)。我們知道,所有對象的父對象都是System.Object對象,這個父對象中有一個MemberwiseClone方法,該方法就可以用來實現(xiàn)淺拷貝,下面具體看看淺拷貝的實現(xiàn)方式,具體演示代碼如下所示:
// 繼承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方法,其實現(xiàn)直接調(diào)用了Object的MemberwiseClone方法來完成淺拷貝,如果想實現(xiàn)深拷貝,也可以在Clone方法中實現(xiàn)深拷貝的邏輯。接下來就是對上面定義的類進行淺拷貝測試了,看看是否是實現(xiàn)的淺拷貝,具體演示代碼如下所示:
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種方式實現(xiàn)的都是淺拷貝 List<PersonA> personsCopy = new List<PersonA>(personList); PersonA[] personCopy2 = new PersonA[2]; personList.CopyTo(personCopy2); // 由于實現(xiàn)的是淺拷貝,所以改變一個對象的值,其他2個對象的值都會發(fā)生改變,因為它們都是使用的同一份實體,即它們指向內(nèi)存中同一個地址 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); } } }
上面代碼的運行結(jié)果如下圖所示:
從上面運行結(jié)果可以看出,.NET中值類型默認是深拷貝的,而對于引用類型,默認實現(xiàn)的是淺拷貝。所以對于類中引用類型的屬性改變時,其另一個對象也會發(fā)生改變。
上面已經(jīng)介紹了淺拷貝的實現(xiàn)方式,那深拷貝要如何實現(xiàn)呢?在前言部分已經(jīng)介紹了,實現(xiàn)深拷貝的方式有:反射、反序列化和表達式樹。在這里,我只介紹反射和反序列化的方式,對于表達式樹的方式在網(wǎng)上也沒有找到,當時面試官說是可以的,如果大家找到了表達式樹的實現(xiàn)方式,麻煩還請留言告知下。下面我們首先來看看反射的實現(xiàn)方式吧:
// 利用反射實現(xiàn)深拷貝 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; }
反序列化的實現(xiàn)方式,反序列化的方式也可以細分為3種,具體的實現(xiàn)如下所示:
// 利用XML序列化和反序列化實現(xiàn) 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; } // 利用二進制序列化和反序列實現(xiàn) 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序列化和反序列化實現(xiàn) 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; } // 表達式樹實現(xiàn) // ....
上面反射的實現(xiàn)方式,對于相互引用的對象會出現(xiàn)StackOverflower的錯誤,由于對象的相互引用,會導致方法循環(huán)調(diào)用。下面就是一個相互引用對象的例子:
[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 }
在面試過程中,針對這個問題的解決方式我回答的是不知道,回來之后思考了之后,也就有了點思路。首先想到的是:能不能用一個字典來記錄每個對象被反射的次數(shù),仔細想想可行,于是開始實現(xiàn),初步修復后的反射實現(xiàn)如下所示:
public class DeepCopyHelper { // 用一個字典來存放每個對象的反射次數(shù)來避免反射代碼的循環(huán)遞歸 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()); } // 對于類類型開始記錄對象反射的次數(shù) 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]; } }
下面用代碼來測試下上面的代碼是否已經(jīng)解決了循環(huán)遞歸的問題,具體的測試代碼如下所示:
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 }; // 使用反序列化來實現(xiàn)深拷貝 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(); } }
此時的運行結(jié)果如下圖所示:
剛開始看到這樣的運行結(jié)果,開心地以為已經(jīng)解決了循環(huán)遞歸的問題了,因為此時結(jié)果成功運行出來了,沒有了StackOverflower的錯誤了。但是仔細一看,反序列化和反射完成的深拷貝的運行結(jié)果不一樣,如上圖中紅色圈出來的部分。顯然,反序列化的結(jié)果是沒有錯誤的,顯然目前實現(xiàn)的反射代碼還是有問題的。接下來就是思考了。為什么上面反射的代碼不正確呢?
仔細分析DeepCopyWithReflection_Second中的代碼,發(fā)現(xiàn)下面代碼紅色部分是錯誤的:
int reflectionCount = Add(typereflectionCountDic, obj.GetType()); if (reflectionCount > 1) return obj; // 是錯誤的
對DeepCopyWithReflection_Second方法仔細分析,在對TestB進行反射時,當反射到DeepCopyClass屬性時,此時會遞歸調(diào)用DeepCopyWithReflection_Second方法,此時在typereflectionCountDic發(fā)現(xiàn)DeepCopyDemoClass已經(jīng)被反射了,則直接返回,這樣分析好像沒什么錯誤,但是此時返回的是deepCopyClassA對象,但是我們需要返回的是deepCopyClassB對象,即此時deepCopyClassB對象的內(nèi)存結(jié)構(gòu)如下圖所示:
而我們其實需要deepCopyClassB對象的內(nèi)存結(jié)構(gòu)如下圖所示:
既然找到了DeepCopyWithReflection_Second的錯誤原因,那我們就要解決了。上面說我們返回的應(yīng)該是deepCopyClassB對象,而我們怎么得到創(chuàng)建的deepCopyClassB對象呢?這里我就想能不能用一個變量來保存一開始通過CreateInstance方法創(chuàng)建的deepCopyClassB對象呢?驗證想法最好的辦法就是代碼了,這樣我就按照這個思路對DeepCopyWithReflection_Second又進行一次改進,最終的代碼如下所示:
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; // 保存一開始創(chuàng)建的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 }; // 使用反序列化來實現(xiàn)深拷貝 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(); } }
此時的運行結(jié)果如下圖示所示:
從上面的測試結(jié)果可以看出,此時深拷貝的反射實現(xiàn)方法基本上沒什么問題了。這個方法也同時解決了相互引用對象的循環(huán)遞歸問題。
到這里,該文章的內(nèi)容就結(jié)束。這里主要記錄下自己在一次面試過程中遇到問題的一次總結(jié),從中可以看出,反射進行深拷貝會有很多其他的問題,所以平時還是建議大家使用序列化的形式來進行深拷貝。
最后附上本文所有×××:DeepCopy.zip
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。