• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >C#教程 > .NET的深复制方法(以C#语言为例)

.NET的深复制方法(以C#语言为例)

作者:北风其凉 字体:[增加 减小] 来源:互联网 时间:2017-05-28

北风其凉 通过本文主要向大家介绍了.net后台调用js方法,.net 扩展方法,vb.net string 方法,o net方法,离线安装net3.5方法等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

很多时候我们复制一个对象实例A到实例B,在用实例B去做其他事情的时候,会对实例B进行修改,为保证对B的修改不会影响到A的正常使用,就需要使用到深复制。

我在网上搜到一些深复制的方法,同时写了几组例子对这些方法进行测试。

我的操作系统版本为Win7旗舰版,.NET Framework版本是4.5

测试程序

我建了一个C#窗体应用程序(Winform),其主窗口FormMain的Load函数内容如下:

private void FormMain_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.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(ex.ToString());
 }

</div>

    //测试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", "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 ex)
 {
  Console.WriteLine(ex.ToString());
 }
</div>

    //测试3:深度复制 DataTable
  

 try
 {
  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.Rows.Add("3-1", "3-2", "3-3");
  Console.WriteLine("=== 复制前 ===");
  for (int i = 0; i < dtKirov.Columns.Count; i++)
  {
   Console.Write(dtKirov.Columns[i].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.Rows[i][j].ToString() + "\t");
   }
   Console.WriteLine();
  }
  Console.WriteLine();
  DataTable dtDreadNought = (DataTable)DataManHelper.DeepCopyObject(dtKirov);
  Console.WriteLine("=== 复制后 ===");
  for (int i = 0; i < dtDreadNought.Columns.Count; i++)
  {
   Console.Write(dtDreadNought.Columns[i].ColumnName + "\t");
  }
  Console.WriteLine("\n-----------------");
  for (int i = 0; i < dtDreadNought.Columns.Count; i++)
  {
   for (int j = 0; j < dtDreadNought.Rows.Count; j++)
   {
    Console.Write(dtDreadNought.Rows[i][j].ToString() + "\t");
   }
   Console.WriteLine();
  }
  Console.WriteLine();
 }
 catch (Exception ex)
 {
  Console.WriteLine(ex.ToString());
 }
</div>

    //测试4:深度复制 TextBox
   

 try
 {
  Console.WriteLine("=== 深度复制 TextBox ===");
  txtTest.Text = "1234";
  Console.WriteLine("复制前:" + txtTest.Text);
  TextBox txtTmp = new TextBox();
  txtTmp = (TextBox)DataManHelper.DeepCopyObject(txtTest);
  Console.WriteLine("复制后:" + txtTmp.Text);
 }
 catch (Exception ex)
 {
  Console.WriteLine(ex.ToString());
 }
</div>

    //测试5:深度复制 DataGridView
 

 try
 {
  Console.WriteLine("=== 深度复制 DataGridView ===");
  DataGridView dgvTmp = new DataGridView();
  dgvTmp = (DataGridView)DataManHelper.DeepCopyObject(dgvTest);
 }
 catch (Exception ex)
 {
  Console.WriteLine(ex.ToString());
 }
}
</div>

其中txtTest是一个测试用的TextBox,dgvTmp是一个测试用的DataGridView,TestClass是一个自定义类,TestClassWithS是添加了Serializable特性的TestClass类,它们的具体实现如下:

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";
   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;
  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 + "\n";
     }
    }
   }
   if (d != null)
   {
    s += d.ToString();
   }
   return s;
  }
 }
}

</div>

我对每个搜来的深复制方法,都用了这五个类的实例进行深复制测试,这五个类的特征如下:

I、对自定义类TestClass进行深复制测试

II、对自定义类TestClassWithS进行深复制测试,TestClassWithS是添加了Serializable特性的TestClass类

III、对DataTable进行深复制测试

IV、对控件TextBox进行深复制测试

V、对控件DataGridView进行深复制测试

我们通过实现方法DataManHelper.DeepCopyObject来进行测试

测试深复制方法1

使用二进制流的序列化与反序列化深度复制对象

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 clonedObj = Formatter.Deserialize(stream);
 stream.Close();
 return clonedObj;
}
</div>

五个场景的测试结果为:

I、触发异常SerializationException,原因是该类不支持序列化

“System.Runtime.Serialization.SerializationException”类型的第一次机会异常在 mscorlib.dll 中发生
System.Runtime.Serialization.SerializationException: 程序集“DataCopyTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null”中的类型“DataCopyTest.TestClass”未标记为可序列化。
   在 System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type)
   在 System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context)
   在 System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo()
   在 System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit s

分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • .NET的深复制方法(以C#语言为例)

相关文章

  • 2017-05-28C#判断指定驱动器是否已经准备就绪的方法
  • 2017-05-28C#七大经典排序算法系列(下)
  • 2017-05-28C#中哈希表(HashTable)用法实例详解(添加/移除/判断/遍历/排序等)
  • 2017-05-28C#路径,文件,目录及IO常见操作汇总
  • 2017-05-28C#简单的向量用法实例教程
  • 2017-05-28解析XPath语法之在C#中使用XPath的示例详解
  • 2017-05-28DevExpress之饼状图突出(Explode)设置实例
  • 2017-05-28C#十六进制字符串转十进制int的方法
  • 2017-05-28基于C# winform实现图片上传功能的方法
  • 2017-05-28DevExpress实现TreeList节点互斥的方法

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • C#之Socket操作类实例解析
    • c#模拟平抛运动动画的方法详解
    • C#实现图像锐化的方法
    • C#面向对象特征的具体实现及作用详解
    • C#多线程学习之(五)使用定时器进行多线程的自动管理
    • C#生成设置范围内的Double类型随机数的方法
    • C#绘制中国国旗的方法
    • C#中Trim()、TrimStart()、TrimEnd()的用法介绍
    • C#保存与读取DataTable信息到XML格式的方法
    • C#通过IComparable实现ListT.sort()排序

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有