NET的深復(fù)制方法(以C#語言為例)_第1頁
NET的深復(fù)制方法(以C#語言為例)_第2頁
NET的深復(fù)制方法(以C#語言為例)_第3頁
NET的深復(fù)制方法(以C#語言為例)_第4頁
NET的深復(fù)制方法(以C#語言為例)_第5頁
已閱讀5頁,還剩4頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、NET的深復(fù)制方法(以C#語言為例)深復(fù)制需要將對(duì)象實(shí)例中字段引用的對(duì)象也進(jìn)行復(fù)制,在平時(shí)的編程工作中經(jīng)常要用到這種復(fù)制方式,下面以c#為例來演示一下方法。很多時(shí)候我們復(fù)制一個(gè)對(duì)象實(shí)例A到實(shí)例B,在用實(shí)例B去做其他事情的時(shí)候,會(huì)對(duì)實(shí)例B進(jìn)行修改,為保證對(duì)B的修改不會(huì)影響到A的正常使用,就需要使用到深復(fù)制。 我在網(wǎng)上搜到一些深復(fù)制的方法,同時(shí)寫了幾組例子對(duì)這些方法進(jìn)行測試。 我的操作系統(tǒng)版本為Win7旗艦版,.NET Framework版本是4.5 測試程序 我建了一個(gè)C#窗體應(yīng)用程序(Winform),其主窗口FormMain的Load函數(shù)內(nèi)容如下:private void FormMain_

2、Load(object sender, EventArgs e) /測試1:深度復(fù)制 自定義類 try Console.WriteLine("= 深度復(fù)制 自定義類 ="); TestClass test1 = new TestClass(); test1.a = 10; test1.b = "hello world!" test1.c = new string "x", "y", "z" ; TestClass test2 = new TestClass(); test2.a = 11; te

3、st2.b = "hello world2!" test2.c = new string "i", "j", "k" ; test1.d = test2; Console.WriteLine("-test1_start-"); Console.WriteLine(test1); Console.WriteLine("-test1_end-"); TestClass test3 = (TestClass)DataManHelper.DeepCopyObject(test1);

4、Console.WriteLine("-test3_start-"); Console.WriteLine(test3); Console.WriteLine("-test3_end-"); catch (Exception ex) Console.WriteLine(ex.ToString(); /測試2:深度復(fù)制 可序列化的自定義類try Console.WriteLine("= 深度復(fù)制 可序列化的自定義類 ="); TestClassWithS test1 = new TestClassWithS(); test1.a = 1

5、0; test1.b = "hello world!" test1.c = new string "x", "y", "z" ; TestClassWithS test2 = new TestClassWithS(); test2.a = 11; test2.b = "hello world2!" test2.c = new string "i", "j", "k" ; test1.d = test2; Console.WriteLin

6、e("-test1_start-"); Console.WriteLine(test1); Console.WriteLine("-test1_end-"); TestClassWithS test3 = (TestClassWithS)DataManHelper.DeepCopyObject(test1); Console.WriteLine("-test3_start-"); Console.WriteLine(test3); Console.WriteLine("-test3_end-");catch (Ex

7、ception ex) Console.WriteLine(ex.ToString(); /測試3:深度復(fù)制 DataTabletry Console.WriteLine("= 深度復(fù)制 DataTable ="); DataTable dtKirov = new DataTable("TestTable"); dtKirov.Columns.Add("Col1"); dtKirov.Columns.Add("Col2"); dtKirov.Columns.Add("Col3"); dtKiro

8、v.Rows.Add("1-1", "1-2", "1-3"); dtKirov.Rows.Add("2-1", "2-2", "2-3"); dtKirov.Rows.Add("3-1", "3-2", "3-3"); Console.WriteLine("= 復(fù)制前 ="); for (int i = 0; i < dtKirov.Columns.Count; i+) Console.W

9、rite(dtKirov.Columnsi.ColumnName + "t"); Console.WriteLine("n-"); for (int i = 0; i < dtKirov.Columns.Count; i+) for (int j = 0; j < dtKirov.Rows.Count; j+) Console.Write(dtKirov.Rowsij.ToString() + "t"); Console.WriteLine(); Console.WriteLine(); DataTable dtDread

10、Nought = (DataTable)DataManHelper.DeepCopyObject(dtKirov); Console.WriteLine("= 復(fù)制后 ="); for (int i = 0; i < dtDreadNought.Columns.Count; i+) Console.Write(dtDreadNought.Columnsi.ColumnName + "t"); Console.WriteLine("n-"); for (int i = 0; i < dtDreadNought.Columns

11、.Count; i+) for (int j = 0; j < dtDreadNought.Rows.Count; j+) Console.Write(dtDreadNought.Rowsij.ToString() + "t"); Console.WriteLine(); Console.WriteLine();catch (Exception ex) Console.WriteLine(ex.ToString(); /測試4:深度復(fù)制 TextBoxtry Console.WriteLine("= 深度復(fù)制 TextBox ="); txtTes

12、t.Text = "1234" Console.WriteLine("復(fù)制前:" + txtTest.Text); TextBox txtTmp = new TextBox(); txtTmp = (TextBox)DataManHelper.DeepCopyObject(txtTest); Console.WriteLine("復(fù)制后:" + txtTmp.Text);catch (Exception ex) Console.WriteLine(ex.ToString(); /測試5:深度復(fù)制 DataGridViewtry Con

13、sole.WriteLine("= 深度復(fù)制 DataGridView ="); DataGridView dgvTmp = new DataGridView(); dgvTmp = (DataGridView)DataManHelper.DeepCopyObject(dgvTest); catch (Exception ex) Console.WriteLine(ex.ToString(); 其中txtTest是一個(gè)測試用的TextBox,dgvTmp是一個(gè)測試用的DataGridView,TestClass是一個(gè)自定義類,TestClassWithS是添加了Serial

14、izable特性的TestClass類,它們的具體實(shí)現(xiàn)如下:using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace DataCopyTest public class TestClass public int a; public string b; public string c; public TestClass d; public override string ToString() string s = "a:" + a + "n

15、" if (b != null) s += "b:" + b + "n" if (c != null) foreach (string tmps in c) if (!string.IsNullOrWhiteSpace(tmps) s += "c:" + tmps + "n" if (d != null) s += d.ToString(); return s; /支持序列化的TestClass Serializable public class TestClassWithS public int a;

16、public string b; public string c; public TestClassWithS d; public override string ToString() string s = "a:" + a + "n" if (b != null) s += "b:" + b + "n" if (c != null) foreach (string tmps in c) if (!string.IsNullOrWhiteSpace(tmps) s += "c:" + tmps

17、+ "n" if (d != null) s += d.ToString(); return s; 我對(duì)每個(gè)搜來的深復(fù)制方法,都用了這五個(gè)類的實(shí)例進(jìn)行深復(fù)制測試,這五個(gè)類的特征如下: I、對(duì)自定義類TestClass進(jìn)行深復(fù)制測試 II、對(duì)自定義類TestClassWithS進(jìn)行深復(fù)制測試,TestClassWithS是添加了Serializable特性的TestClass類 III、對(duì)DataTable進(jìn)行深復(fù)制測試 IV、對(duì)控件TextBox進(jìn)行深復(fù)制測試 V、對(duì)控件DataGridView進(jìn)行深復(fù)制測試 我們通過實(shí)現(xiàn)方法DataManHelper.DeepCopyOb

18、ject來進(jìn)行測試 測試深復(fù)制方法1 使用二進(jìn)制流的序列化與反序列化深度復(fù)制對(duì)象public static object DeepCopyObject(object obj) BinaryFormatter Formatter = new BinaryFormatter(null, new StreamingContext(StreamingContextStates.Clone); MemoryStream stream = new MemoryStream(); Formatter.Serialize(stream, obj); stream.Position = 0; object cl

19、onedObj = Formatter.Deserialize(stream); stream.Close(); return clonedObj; 五個(gè)場景的測試結(jié)果為: I、觸發(fā)異常SerializationException,原因是該類不支持序列化 “System.Runtime.Serialization.SerializationException”類型的第一次機(jī)會(huì)異常在 mscorlib.dll 中發(fā)生 System.Runtime.Serialization.SerializationException: 程序集“DataCopyTest, Version=, Cu

20、lture=neutral, PublicKeyToken=null”中的類型“DataCopyTest.TestClass”未標(biāo)記為可序列化。 在 System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type) 在 System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context) 在 System.Runtime.Ser

21、ialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo() 在 System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter ob

22、jectWriter, SerializationBinder binder) 在 System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, S“DataCopyTvsh

23、ost.exe”(托管(v4.0.30319): 已加載“C:WindowsMicrosoft.NetassemblyGAC_MSILSystem.Numericsv4.0__b77a5c561934e089System.Numerics.dll” erializationBinder binder) 在 System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header inHeaders, _BinaryWriter serWriter, Boolean fChe

24、ck) 在 System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header headers, Boolean fCheck) 在 System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph) 在 DataCopyTest.DataManHelper.D

25、eepCopyObject(Object obj) 位置 d:MyProgramsDataCopyTestDataCopyTestDataManHelper.cs:行號(hào) 24 在 DataCopyTest.FormMain.FormMain_Load(Object sender, EventArgs e) 位置 d:MyProgramsDataCopyTestDataCopyTestFormMain.cs:行號(hào) 37 II、可正常復(fù)制 () III、可正常復(fù)制 () IV、觸發(fā)異常SerializationException,原因是該類不支持序列化 “System.Runtime.Serial

26、ization.SerializationException”類型的第一次機(jī)會(huì)異常在 mscorlib.dll 中發(fā)生 System.Runtime.Serialization.SerializationException: 程序集“System.Windows.Forms, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089”中的類型“System.Windows.Forms.TextBox”未標(biāo)記為可序列化。 在 System.Runtime.Serialization.FormatterServices.In

27、ternalGetSerializableMembers(RuntimeType type) 在 System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context) 在 System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo() 在 System.Runtime.Serialization.Formatters.Binary.WriteObjectIn

28、fo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder) 在 System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(Object ob

29、j, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder) 在 System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header inHeaders, _Binar

30、yWriter serWriter, Boolean fCheck) 在 System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header headers, Boolean fCheck) 在 System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph)

31、 在 DataCopyTest.DataManHelper.DeepCopyObject(Object obj) 位置 d:MyProgramsDataCopyTestDataCopyTestDataManHelper.cs:行號(hào) 24 在 DataCopyTest.FormMain.FormMain_Load(Object sender, EventArgs e) 位置 d:MyProgramsDataCopyTestDataCopyTestFormMain.cs:行號(hào) 128 V、觸發(fā)異常SerializationException,原因是該類不支持序列化 “System.Runtime.

32、Serialization.SerializationException”類型的第一次機(jī)會(huì)異常在 mscorlib.dll 中發(fā)生 System.Runtime.Serialization.SerializationException: 程序集“System.Windows.Forms, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089”中的類型“System.Windows.Forms.DataGridView”未標(biāo)記為可序列化。 在 System.Runtime.Serialization.Formatter

33、Services.InternalGetSerializableMembers(RuntimeType type) 在 System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context) 在 System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo() 在 System.Runtime.Serialization.Formatters.Binary.Wr

34、iteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder) 在 System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serializ

35、e(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder) 在 System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header inHead

36、ers, _BinaryWriter serWriter, Boolean fCheck) 在 System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header headers, Boolean fCheck) 在 System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Ob

37、ject graph) 在 DataCopyTest.DataManHelper.DeepCopyObject(Object obj) 位置 d:MyProgramsDataCopyTestDataCopyTestDataManHelper.cs:行號(hào) 24 在 DataCopyTest.FormMain.FormMain_Load(Object sender, EventArgs e) 位置 d:MyProgramsDataCopyTestDataCopyTestFormMain.cs:行號(hào) 141 結(jié)論:利用序列化與反序列化到二進(jìn)制流的方法深復(fù)制對(duì)象,只有在該對(duì)象支持Serializabl

38、e特性時(shí)才可使用 測試深復(fù)制方法2public static object DeepCopyObject(object obj) Type t = obj.GetType(); PropertyInfo properties = t.GetProperties(); Object p = t.InvokeMember("", System.Reflection.BindingFlags.CreateInstance, null, obj, null); foreach (PropertyInfo pi in properties) if (pi.CanWrite) obje

39、ct value = pi.GetValue(obj, null); pi.SetValue(p, value, null); return p; 五個(gè)場景的測試結(jié)果為: I、不會(huì)觸發(fā)異常,但結(jié)果完全錯(cuò)誤 II、不會(huì)觸發(fā)異常,但結(jié)果完全錯(cuò)誤 III、不會(huì)觸發(fā)異常,但結(jié)果完全錯(cuò)誤 IV、Text字段賦值結(jié)果正確,但其他內(nèi)容不能保證 V、觸發(fā)異常ArgumentOutOfRangeException、TargetInvocationException “System.ArgumentOutOfRangeException”類型的第一次機(jī)會(huì)異常在 System.Windows.Forms.dll 中

40、發(fā)生 “System.Reflection.TargetInvocationException”類型的第一次機(jī)會(huì)異常在 mscorlib.dll 中發(fā)生 System.Reflection.TargetInvocationException: 調(diào)用的目標(biāo)發(fā)生了異常。 -> System.ArgumentOutOfRangeException: 指定的參數(shù)已超出有效值的范圍。 參數(shù)名: value 在 System.Windows.Forms.DataGridView.set_FirstDisplayedScrollingColumnIndex(Int32 value) - 內(nèi)部異常堆棧跟

41、蹤的結(jié)尾 - 在 System.RuntimeMethodHandle.InvokeMethod(Object target, Object arguments, Signature sig, Boolean constructor) 在 System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object parameters, Object arguments) 在 System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invo

42、keAttr, Binder binder, Object parameters, CultureInfo culture) 在 System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, Object index, CultureInfo culture) 在 System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, Object index

43、) 在 DataCopyTest.DataManHelper.DeepCopyObject(Object obj) 位置 d:MyProgramsDataCopyTestDataCopyTestDataManHelper.cs:行號(hào) 29 在 DataCopyTest.FormMain.FormMain_Load(Object sender, EventArgs e) 位置 d:MyProgramsDataCopyTestDataCopyTestFormMain.cs:行號(hào) 141 結(jié)論:使用這種方法進(jìn)行所謂深復(fù)制,完全是自尋死路! 測試深復(fù)制方法3public static object D

44、eepCopyObject(object obj) if (obj != null) object result = Activator.CreateInstance(obj.GetType(); foreach (FieldInfo field in obj.GetType().GetFields() if (field.FieldType.GetInterface("IList", false) = null) field.SetValue(result, field.GetValue(obj); else IList listObject = (IList)field

45、.GetValue(result); if (listObject != null) foreach (object item in (IList)field.GetValue(obj) listObject.Add(DeepCopyObject(item); return result; else return null; 五個(gè)場景的測試結(jié)果為: I、可正常復(fù)制() II、可正常復(fù)制() III、未觸發(fā)異常, 復(fù)制后DataTable無行列 IV、未觸發(fā)異常,Text字段未賦值 V、未觸發(fā)異常 結(jié)論:這個(gè)方法只適用于深復(fù)制具備簡單結(jié)構(gòu)的類(如類中只有基礎(chǔ)字段、數(shù)組等),對(duì)于不支持序列化的對(duì)象

46、也可以進(jìn)行深復(fù)制。 測試深復(fù)制方法4 這段代碼來源同方法3public static object DeepCopyObject(object obj) if (obj = null) return null; Type type = obj.GetType(); if (type.IsValueType | type = typeof(string) return obj; else if (type.IsArray) Type elementType = Type.GetType( type.FullName.Replace("", string.Empty); var

47、 array = obj as Array; Array copied = Array.CreateInstance(elementType, array.Length); for (int i = 0; i < array.Length; i+) copied.SetValue(DeepCopyObject(array.GetValue(i), i); return Convert.ChangeType(copied, obj.GetType(); else if (type.IsClass) object toret = Activator.CreateInstance(obj.Ge

48、tType(); FieldInfo fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); foreach (FieldInfo field in fields) object fieldValue = field.GetValue(obj); if (fieldValue = null) continue; field.SetValue(toret, DeepCopyObject(fieldValue); return toret; else throw n

49、ew ArgumentException("Unknown type"); 五個(gè)場景的測試結(jié)果為: I、可正常復(fù)制() II、可正常復(fù)制() III、觸發(fā)異常MissingMethodException “System.MissingMethodException”類型的第一次機(jī)會(huì)異常在 mscorlib.dll 中發(fā)生 System.MissingMethodException: 沒有為該對(duì)象定義無參數(shù)的構(gòu)造函數(shù)。 在 System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly,

50、 Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) 在 System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) 在 System.RuntimeType.CreateInstanceDefaultCtor(Bool

51、ean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) 在 System.Activator.CreateInstance(Type type, Boolean nonPublic) 在 System.Activator.CreateInstance(Type type) 在 DataCopyTest.DataManHelper.DeepCopyObject(Object obj) 位置 d:MyProgramsDataCopyTestDataCopyTestDataMan

52、Helper.cs:行號(hào) 45 在 DataCopyTest.DataManHelper.DeepCopyObject(Object obj) 位置 d:MyProgramsDataCopyTestDataCopyTestDataManHelper.cs:行號(hào) 53 在 DataCopyTest.FormMain.FormMain_Load(Object sender, EventArgs e) 位置 d:MyProgramsDataCopyTestDataCopyTestFormMain.cs:行號(hào) 99 IV、未觸發(fā)異常,但Text字段也未賦值成功 V、觸發(fā)異常MissingMethodE

53、xception “System.MissingMethodException”類型的第一次機(jī)會(huì)異常在 mscorlib.dll 中發(fā)生 System.MissingMethodException: 沒有為該對(duì)象定義無參數(shù)的構(gòu)造函數(shù)。 在 System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSe

54、curityCheck) 在 System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) 在 System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) 在 System.Activ

55、ator.CreateInstance(Type type, Boolean nonPublic) 在 System.Activator.CreateInstance(Type type) 在 DataCopyTest.DataManHelper.DeepCopyObject(Object obj) 位置 d:MyProgramsDataCopyTestDataCopyTestDataManHelper.cs:行號(hào) 45 在 DataCopyTest.DataManHelper.DeepCopyObject(Object obj) 位置 d:MyProgramsDataCopyTestDataCopyTestDataManHelpe

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論