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

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

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

2、Load(object sender, EventArgs e) /測試1:深度復制 自定義類 try Console.WriteLine(= 深度復制 自定義類 =); 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; test2.b = hello world2!; test2.c = new string i, j, k ; test1

3、.d = test2; Console.WriteLine(-test1_start-); Console.WriteLine(test1); Console.WriteLine(-test1_end-); TestClass test3 = (TestClass)DataManHelper.DeepCopyObject(test1); Console.WriteLine(-test3_start-); Console.WriteLine(test3); Console.WriteLine(-test3_end-); catch (Exception ex) Console.WriteLine

4、(ex.ToString(); /測試2:深度復制 可序列化的自定義類try Console.WriteLine(= 深度復制 可序列化的自定義類 =); TestClassWithS test1 = new TestClassWithS(); test1.a = 10; 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,

5、j, k ; test1.d = test2; Console.WriteLine(-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 (Exception

6、ex) Console.WriteLine(ex.ToString(); /測試3:深度復制 DataTabletry Console.WriteLine(= 深度復制 DataTable =); DataTable dtKirov = new DataTable(TestTable); dtKirov.Columns.Add(Col1); dtKirov.Columns.Add(Col2); dtKirov.Columns.Add(Col3); dtKirov.Rows.Add(1-1, 1-2, 1-3); dtKirov.Rows.Add(2-1, 2-2, 2-3); dtKirov.

7、Rows.Add(3-1, 3-2, 3-3); Console.WriteLine(= 復制前 =); for (int i = 0; i dtKirov.Columns.Count; i+) Console.Write(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)

8、; Console.WriteLine(); Console.WriteLine(); DataTable dtDreadNought = (DataTable)DataManHelper.DeepCopyObject(dtKirov); Console.WriteLine(= 復制后 =); for (int i = 0; i dtDreadNought.Columns.Count; i+) Console.Write(dtDreadNought.Columnsi.ColumnName + t); Console.WriteLine(n-); for (int i = 0; i dtDrea

9、dNought.Columns.Count; i+) for (int j = 0; j System.ArgumentOutOfRangeException: 指定的參數(shù)已超出有效值的范圍。 參數(shù)名: value 在 System.Windows.Forms.DataGridView.set_FirstDisplayedScrollingColumnIndex(Int32 value) - 內部異常堆棧跟蹤的結尾 - 在 System.RuntimeMethodHandle.InvokeMethod(Object target, Object arguments, Signature sig

10、, Boolean constructor) 在 System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object parameters, Object arguments) 在 System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture) 在 System.Reflection.RuntimePrope

11、rtyInfo.SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, Object index, CultureInfo culture) 在 System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, Object index) 在 DataCopyTest.DataManHelper.DeepCopyObject(Object obj) 位置 d:MyProgramsDataCopyTestDataCopyTe

12、stDataManHelper.cs:行號 29 在 DataCopyTest.FormMain.FormMain_Load(Object sender, EventArgs e) 位置 d:MyProgramsDataCopyTestDataCopyTestFormMain.cs:行號 141 結論:使用這種方法進行所謂深復制,完全是自尋死路! 測試深復制方法3public static object DeepCopyObject(object obj) if (obj != null) object result = Activator.CreateInstance(obj.GetType

13、(); 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.GetValue(result); if (listObject != null) foreach (object item in (IList)field.GetValue(obj) listObject.Ad

14、d(DeepCopyObject(item); return result; else return null; 五個場景的測試結果為: I、可正常復制() II、可正常復制() III、未觸發(fā)異常, 復制后DataTable無行列 IV、未觸發(fā)異常,Text字段未賦值 V、未觸發(fā)異常 結論:這個方法只適用于深復制具備簡單結構的類(如類中只有基礎字段、數(shù)組等),對于不支持序列化的對象也可以進行深復制。 測試深復制方法4 這段代碼來源同方法3public static object DeepCopyObject(object obj) if (obj = null) return null; T

15、ype 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 array = obj as Array; Array copied = Array.CreateInstance(elementType, array.Length); for (int i = 0; i array.Length;

16、i+) copied.SetValue(DeepCopyObject(array.GetValue(i), i); return Convert.ChangeType(copied, obj.GetType(); else if (type.IsClass) object toret = Activator.CreateInstance(obj.GetType(); FieldInfo fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); foreach (F

17、ieldInfo field in fields) object fieldValue = field.GetValue(obj); if (fieldValue = null) continue; field.SetValue(toret, DeepCopyObject(fieldValue); return toret; else throw new ArgumentException(Unknown type); 五個場景的測試結果為: I、可正常復制() II、可正常復制() III、觸發(fā)異常MissingMethodException “System.MissingMethodExc

18、eption”類型的第一次機會異常在 mscorlib.dll 中發(fā)生 System.MissingMethodException: 沒有為該對象定義無參數(shù)的構造函數(shù)。 在 System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) 在 System.RuntimeType.CreateInsta

19、nceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) 在 System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) 在 System.Activator.CreateInstance(Type type, Boolean nonPublic) 在 S

20、ystem.Activator.CreateInstance(Type type) 在 DataCopyTest.DataManHelper.DeepCopyObject(Object obj) 位置 d:MyProgramsDataCopyTestDataCopyTestDataManHelper.cs:行號 45 在 DataCopyTest.DataManHelper.DeepCopyObject(Object obj) 位置 d:MyProgramsDataCopyTestDataCopyTestDataManHelper.cs:行號 53 在 DataCopyTest.FormMai

21、n.FormMain_Load(Object sender, EventArgs e) 位置 d:MyProgramsDataCopyTestDataCopyTestFormMain.cs:行號 99 IV、未觸發(fā)異常,但Text字段也未賦值成功 V、觸發(fā)異常MissingMethodException “System.MissingMethodException”類型的第一次機會異常在 mscorlib.dll 中發(fā)生 System.MissingMethodException: 沒有為該對象定義無參數(shù)的構造函數(shù)。 在 System.RuntimeTypeHandle.CreateInsta

22、nce(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) 在 System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) 在 System.RuntimeType.Creat

23、eInstanceDefaultCtor(Boolean 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:MyProgramsDataCopyTe

24、stDataCopyTestDataManHelper.cs:行號 45 在 DataCopyTest.DataManHelper.DeepCopyObject(Object obj) 位置 d:MyProgramsDataCopyTestDataCopyTestDataManHelper.cs:行號 53 在 DataCopyTest.FormMain.FormMain_Load(Object sender, EventArgs e) 位置 d:MyProgramsDataCopyTestDataCopyTestFormMain.cs:行號 141 結論:這個方法的作用類似方法3,只能深復制

25、基本數(shù)據(jù)類型組成的類 具體問題具體分析 從上面的例子可以看出,想找一個放之四海而皆準的方式去深復制所有對象是很困難的。一些使用高級語言特性(反射)的深復制方法,即使可以在部分類上試用成功,也無法對所有的類都具備十足的把握。因此我認為應該采取下面的方式處理對象的深復制問題: 1、對于由基本數(shù)據(jù)類型組成的類,為之打上Serializable標簽,直接使用序列化與反序列化的方法進行深復制 2、其他較為復雜的類型如DataGridView,可根據(jù)自身情況寫一個方法進行深復制,之所以在這里說要根據(jù)自身情況寫方法,是因為在對很多類進行復制時,你只需要復制對你有用的屬性就行了。如TextBox控件中,只有T

26、ext一個屬性對你是有用的,如果你需要在復制后的對象中用到Readonly等屬性的值,那么在你自己實現(xiàn)的復制方法中,也加上對這些屬性的賦值即可。這樣做還有一個好處,就是方便進行一些定制化的開發(fā)。 如下面這段代碼,就是對DataGridView的一個近似的深復制,這段代碼將一個DataGridView(dgv)的內容復制到另一個DataGridView(dgvTmp)中,然后將dgvTmp傳遞給相關函數(shù)用于將DataGridView中的內容輸出到Excel文檔:DataGridView dgvTmp = new DataGridView();dgvTmp.AllowUserToAddRows = false; /不允許用戶生成行,否則導出后會多出最后一行for (int i = 0; i dgv.Columns.Count; i+) dgvTmp.Columns.Add(dgv.Columnsi.Name, dgv.Columnsi.HeaderText); if (dgv.Columnsi.Defa

溫馨提示

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

評論

0/150

提交評論