這個(gè)星期參加了一個(gè)面試,面試中問到深淺拷貝的區(qū)別,然后我就簡單了講述了它們的之間的區(qū)別,然后面試官又繼續(xù)問,如何實(shí)現(xiàn)一個(gè)深拷貝呢?當(dāng)時(shí)只回答回答了一種方式,就是使用反射,然后面試官提示還可以通過反序列化和表達(dá)樹的方式。然后又繼續(xù)問,如果用反射來實(shí)現(xiàn)深拷貝的話,如何解決互相引用對(duì)象的問題呢? 當(dāng)時(shí)我給出的答案是說那就不用反射去實(shí)現(xiàn)唄,用反序列化實(shí)現(xiàn)唄,或者直接避免使兩個(gè)對(duì)象互相引用唄。然后面試官說,如果一定用反射來寫,你是怎么去解決這個(gè)問題呢?這時(shí)候我就愣住了。
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對(duì)這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長期合作伙伴,公司提供的服務(wù)項(xiàng)目有:主機(jī)域名、虛擬空間、營銷軟件、網(wǎng)站建設(shè)、三亞網(wǎng)站維護(hù)、網(wǎng)站推廣。
這樣也就有了這篇文章。今天就來深入解析下深淺拷貝的問題。
首先,講到深淺拷貝,自然就有一個(gè)問題來了?什么是深拷貝,什么又是淺拷貝呢?下面就具體介紹下它們的定義。
深拷貝:指的是拷貝一個(gè)對(duì)象時(shí),不僅僅把對(duì)象的引用進(jìn)行復(fù)制,還把該對(duì)象引用的值也一起拷貝。這樣進(jìn)行深拷貝后的拷貝對(duì)象就和源對(duì)象互相獨(dú)立,其中任何一個(gè)對(duì)象的改動(dòng)都不會(huì)對(duì)另外一個(gè)對(duì)象造成影響。舉個(gè)例子,一個(gè)人叫張三,然后使用克隆技術(shù)以張三來克隆另外一個(gè)人叫李四,這樣張三和李四就是相互獨(dú)立的,不管張三缺胳膊還是李四少腿了都不會(huì)影響另外一個(gè)人。在.NET領(lǐng)域,值對(duì)象就是典型的例子,如int, Double以及結(jié)構(gòu)體和枚舉等。具體例子如下所示:
int source = 123; // 值類型賦值內(nèi)部執(zhí)行深拷貝 int copy = source; // 對(duì)拷貝對(duì)象進(jìn)行賦值不會(huì)改變?cè)磳?duì)象的值 copy = 234; // 同樣對(duì)源對(duì)象賦值也不會(huì)改變拷貝對(duì)象的值 source = 345;
淺拷貝:指的是拷貝一個(gè)對(duì)象時(shí),僅僅拷貝對(duì)象的引用進(jìn)行拷貝,但是拷貝對(duì)象和源對(duì)象還是引用同一份實(shí)體。此時(shí),其中一個(gè)對(duì)象的改變都會(huì)影響到另一個(gè)對(duì)象。例如,一個(gè)人一開始叫張三,后來改名字為張老三了,可是他們還是同一個(gè)人,不管張三缺胳膊還是張老三少腿,都反應(yīng)在同一個(gè)人身上。在.NET中引用類型就是一個(gè)例子。如類類型。具體例子如下所示:
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 = "張老三"; // 拷貝對(duì)象改變Name值 // 結(jié)果都是"張老三",因?yàn)閷?shí)現(xiàn)的是淺拷貝,一個(gè)對(duì)象的改變都會(huì)影響到另一個(gè)對(duì)象 Console.WriteLine("Person.Name: [SourceP: {0}] [CopyP:{1}]", sourceP.Name, copyP.Name); Console.Read(); } }
上面已經(jīng)明白了深淺拷貝的定義,至于他們之間的區(qū)別也在定義中也有所體現(xiàn)。介紹完了它們的定義和區(qū)別之后,自然也就有了如何去實(shí)現(xiàn)它們呢?
對(duì)于,淺拷貝的實(shí)現(xiàn)方式很簡單,.NET自身也提供了實(shí)現(xiàn)。我們知道,所有對(duì)象的父對(duì)象都是System.Object對(duì)象,這個(gè)父對(duì)象中有一個(gè)MemberwiseClone方法,該方法就可以用來實(shí)現(xiàn)淺拷貝,下面具體看看淺拷貝的實(shí)現(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方法,其實(shí)現(xiàn)直接調(diào)用了Object的MemberwiseClone方法來完成淺拷貝,如果想實(shí)現(xiàn)深拷貝,也可以在Clone方法中實(shí)現(xiàn)深拷貝的邏輯。接下來就是對(duì)上面定義的類進(jìn)行淺拷貝測試了,看看是否是實(shí)現(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種方式實(shí)現(xiàn)的都是淺拷貝 List<PersonA> personsCopy = new List<PersonA>(personList); PersonA[] personCopy2 = new PersonA[2]; personList.CopyTo(personCopy2); // 由于實(shí)現(xiàn)的是淺拷貝,所以改變一個(gè)對(duì)象的值,其他2個(gè)對(duì)象的值都會(huì)發(fā)生改變,因?yàn)樗鼈兌际鞘褂玫耐环輰?shí)體,即它們指向內(nèi)存中同一個(gè)地址 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); } } }
上面代碼的運(yùn)行結(jié)果如下圖所示:
從上面運(yùn)行結(jié)果可以看出,.NET中值類型默認(rèn)是深拷貝的,而對(duì)于引用類型,默認(rèn)實(shí)現(xiàn)的是淺拷貝。所以對(duì)于類中引用類型的屬性改變時(shí),其另一個(gè)對(duì)象也會(huì)發(fā)生改變。
上面已經(jīng)介紹了淺拷貝的實(shí)現(xiàn)方式,那深拷貝要如何實(shí)現(xiàn)呢?在前言部分已經(jīng)介紹了,實(shí)現(xiàn)深拷貝的方式有:反射、反序列化和表達(dá)式樹。在這里,我只介紹反射和反序列化的方式,對(duì)于表達(dá)式樹的方式在網(wǎng)上也沒有找到,當(dāng)時(shí)面試官說是可以的,如果大家找到了表達(dá)式樹的實(shí)現(xiàn)方式,麻煩還請(qǐng)留言告知下。下面我們首先來看看反射的實(shí)現(xiàn)方式吧:
// 利用反射實(shí)現(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; }
反序列化的實(shí)現(xiàn)方式,反序列化的方式也可以細(xì)分為3種,具體的實(shí)現(xiàn)如下所示:
// 利用XML序列化和反序列化實(shí)現(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; } // 利用二進(jìn)制序列化和反序列實(shí)現(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); // 反序列化成對(duì)象 retval = bf.Deserialize(ms); ms.Close(); } return (T)retval; } // 利用DataContractSerializer序列化和反序列化實(shí)現(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; } // 表達(dá)式樹實(shí)現(xiàn) // ....
上面反射的實(shí)現(xiàn)方式,對(duì)于相互引用的對(duì)象會(huì)出現(xiàn)StackOverflower的錯(cuò)誤,由于對(duì)象的相互引用,會(huì)導(dǎo)致方法循環(huán)調(diào)用。下面就是一個(gè)相互引用對(duì)象的例子:
[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對(duì)象,TestB類又引用了DeepCopyDemoClass對(duì)象,從而造成了相互引用 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 }
在面試過程中,針對(duì)這個(gè)問題的解決方式我回答的是不知道,回來之后思考了之后,也就有了點(diǎn)思路。首先想到的是:能不能用一個(gè)字典來記錄每個(gè)對(duì)象被反射的次數(shù),仔細(xì)想想可行,于是開始實(shí)現(xiàn),初步修復(fù)后的反射實(shí)現(xiàn)如下所示:
public class DeepCopyHelper { // 用一個(gè)字典來存放每個(gè)對(duì)象的反射次數(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()); } // 對(duì)于類類型開始記錄對(duì)象反射的次數(shù) int reflectionCount = Add(typereflectionCountDic, obj.GetType()); if (reflectionCount > 1) return obj; // 這里有錯(cuò)誤 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 }; // 使用反序列化來實(shí)現(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(); } }
此時(shí)的運(yùn)行結(jié)果如下圖所示:
剛開始看到這樣的運(yùn)行結(jié)果,開心地以為已經(jīng)解決了循環(huán)遞歸的問題了,因?yàn)榇藭r(shí)結(jié)果成功運(yùn)行出來了,沒有了StackOverflower的錯(cuò)誤了。但是仔細(xì)一看,反序列化和反射完成的深拷貝的運(yùn)行結(jié)果不一樣,如上圖中紅色圈出來的部分。顯然,反序列化的結(jié)果是沒有錯(cuò)誤的,顯然目前實(shí)現(xiàn)的反射代碼還是有問題的。接下來就是思考了。為什么上面反射的代碼不正確呢?
仔細(xì)分析DeepCopyWithReflection_Second中的代碼,發(fā)現(xiàn)下面代碼紅色部分是錯(cuò)誤的:
int reflectionCount = Add(typereflectionCountDic, obj.GetType()); if (reflectionCount > 1) return obj; // 是錯(cuò)誤的
對(duì)DeepCopyWithReflection_Second方法仔細(xì)分析,在對(duì)TestB進(jìn)行反射時(shí),當(dāng)反射到DeepCopyClass屬性時(shí),此時(shí)會(huì)遞歸調(diào)用DeepCopyWithReflection_Second方法,此時(shí)在typereflectionCountDic發(fā)現(xiàn)DeepCopyDemoClass已經(jīng)被反射了,則直接返回,這樣分析好像沒什么錯(cuò)誤,但是此時(shí)返回的是deepCopyClassA對(duì)象,但是我們需要返回的是deepCopyClassB對(duì)象,即此時(shí)deepCopyClassB對(duì)象的內(nèi)存結(jié)構(gòu)如下圖所示:
而我們其實(shí)需要deepCopyClassB對(duì)象的內(nèi)存結(jié)構(gòu)如下圖所示:
既然找到了DeepCopyWithReflection_Second的錯(cuò)誤原因,那我們就要解決了。上面說我們返回的應(yīng)該是deepCopyClassB對(duì)象,而我們?cè)趺吹玫絼?chuàng)建的deepCopyClassB對(duì)象呢?這里我就想能不能用一個(gè)變量來保存一開始通過CreateInstance方法創(chuàng)建的deepCopyClassB對(duì)象呢?驗(yàn)證想法最好的辦法就是代碼了,這樣我就按照這個(gè)思路對(duì)DeepCopyWithReflection_Second又進(jìn)行一次改進(jìn),最終的代碼如下所示:
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對(duì)象 object retval = Activator.CreateInstance(obj.GetType()); if(retval.GetType() == typeof(DeepCopyDemoClass)) DeepCopyDemoClasstypeRef = retval; // 保存一開始創(chuàng)建的DeepCopyDemoClass對(duì)象 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 }; // 使用反序列化來實(shí)現(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(); } }
此時(shí)的運(yùn)行結(jié)果如下圖示所示:
從上面的測試結(jié)果可以看出,此時(shí)深拷貝的反射實(shí)現(xiàn)方法基本上沒什么問題了。這個(gè)方法也同時(shí)解決了相互引用對(duì)象的循環(huán)遞歸問題。
到這里,該文章的內(nèi)容就結(jié)束。這里主要記錄下自己在一次面試過程中遇到問題的一次總結(jié),從中可以看出,反射進(jìn)行深拷貝會(huì)有很多其他的問題,所以平時(shí)還是建議大家使用序列化的形式來進(jìn)行深拷貝。
最后附上本文所有×××:DeepCopy.zip
當(dāng)前標(biāo)題:[C#進(jìn)階系列]專題一:深入解析深拷貝和淺拷貝
網(wǎng)頁路徑:http://jinyejixie.com/article12/psigdc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、網(wǎng)站營銷、靜態(tài)網(wǎng)站、企業(yè)建站、標(biāo)簽優(yōu)化、關(guān)鍵詞優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)