c#:浅克隆和深克隆,序列化和反序列化
一、浅克隆和深克隆(浅复制和深复制)
浅克隆和深克隆最典型的应用是数据集对象DataSet的Clone和Copy方法。Clone()方法用来复制DataSet的结构,但是不复制DataSet的数据,实现了浅复制。Copy()方法不但复制结构,也复制数据,实现了深复制。另外:我们熟知的Object对象有一个MemberwiseClone()方法,它实现的就是浅表复制。该方法不是虚拟的,所以不能重写它的实现代码。
1、浅克隆:被复制对象的所有变量都含有与原来的对象相同的值,而所有的对其他对象的引用都仍然指向原来的对象。
Code is cheap.看代码:
(1)值类型的浅克隆
using System; namespace ShallowColone
{
/*浅克隆 只有值类型的情况*/
class SourceClass : ICloneable //继承自ICloneable,创建作为当前实例副本的新对象。
{
public SourceClass(string name,int id)
{
this.name = name;
this.id = id;
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private int id; public int Id
{
get { return id; }
set { id = value; }
} public object Clone()
{
return this.MemberwiseClone();
}
} class Test
{
static void Main()
{
SourceClass test = new SourceClass("source class",1);
SourceClass test2 = (SourceClass)test.Clone();
Console.WriteLine("test name:" + test.Name);
Console.WriteLine("test2 name:" + test2.Name);
Console.WriteLine("test id:" + test.Id);
Console.WriteLine("test2 id:" + test2.Id);
if (test.Name == test2.Name && test.Id == test2.Id)
{
Console.WriteLine("See,the value after the clone method is the same!");
}
Console.WriteLine(object.ReferenceEquals(test, test2)); //对象的一个副本,内存地址不是指向同一个引用
test2.Name = "copyied class";
Console.WriteLine("test name:" + test.Id);
Console.WriteLine("test2 name:" + test2.Id);
Console.ReadLine();
}
}
}
ps:注意string类型是比较特殊的值类型
(2)引用类型的浅克隆
using System; namespace ShallowColone
{
class User
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
} /*浅克隆 值类型和引用类型的情况*/
class SourceClass : ICloneable //继承自ICloneable,创建作为当前实例副本的新对象。
{
public SourceClass(string name,int id)
{
this.name = name;
this.id = id;
testUser = new User();//初始化一个引用对象
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private int id; public int Id
{
get { return id; }
set { id = value; }
} private User testUser; public void SetUser(string userName)
{
testUser.Name = userName;
} public object Clone()
{
return this.MemberwiseClone();
}
public void Display()
{
Console.WriteLine("Hi,i am " + testUser.Name);
}
} class Test
{
static void Main()
{ SourceClass test = new SourceClass("source class", 1);
test.SetUser("Jeff Wong"); SourceClass test2 = (SourceClass)test.Clone(); test2.SetUser("jeff"); SourceClass test3 = (SourceClass)test.Clone(); test3.SetUser("jeff2"); //下面说明三个实例化对象都是指向同一个引用(最后的那个人jeff2)
test.Display();
test2.Display();
test3.Display(); Console.WriteLine("See,the reference after the clone method is the same!");
Console.ReadLine();
}
}
}
2、深克隆:对于值类型,被复制对象的所有变量都含有与原来的对象相同的值,把要复制的对象的引用的对象都复制一遍。
也就是说,深克隆的值类型和浅克隆一样,但是对于引用类型,深克隆把引用对象的变量指向复制过的新对象,而不是原有的引用的对象。下面改进“引用类型的浅复制”中的示例来说明深克隆的概念:
using System; namespace ShallowColone
{
class User : ICloneable //用户也继承自ICloneable接口
{
private string name;
public string Name
{
get { return name; }
set { name = value; }
} public object Clone()
{
return (User)this.MemberwiseClone();
}
} /*浅克隆 值类型和引用类型的情况*/
class SourceClass : ICloneable //继承自ICloneable,创建作为当前实例副本的新对象。
{
public SourceClass(string name,int id)
{
this.name = name;
this.id = id;
testUser =new User();//***初始化一个引用对象,clone方法***
}
/// <summary>
/// 提供clone方法调用的私有构造函数,以便克隆用户信息
/// </summary>
/// <param name="testUser"></param>
private SourceClass(User testUser)
{
this.testUser = (User)testUser.Clone();
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private int id; public int Id
{
get { return id; }
set { id = value; }
} private User testUser; public void SetUser(string userName)
{
testUser.Name = userName;
} /// <summary>
/// 深克隆 改进后的克隆方法
/// </summary>
/// <returns></returns>
public object Clone()
{
SourceClass retClass = new SourceClass(this.testUser);//调用私有构造函数,让用户克隆完成
retClass.id = this.id;
retClass.name = this.name;
return retClass;
}
public void Display()
{
Console.WriteLine("Hi,i am " + testUser.Name);
}
} class Test
{
static void Main()
{ SourceClass test = new SourceClass("source class", 1);
test.SetUser("Jeff Wong"); SourceClass test2 = (SourceClass)test.Clone(); test2.SetUser("jeff"); SourceClass test3 = (SourceClass)test.Clone(); test3.SetUser("jeff2"); //经过深克隆,达到我们设置不同用户的初衷
test.Display();
test2.Display();
test3.Display(); Console.WriteLine("See,the reference after the clone method is the NOT same!");
Console.ReadLine();
}
}
}
二、序列化和反序列化
前言: 当两个进程在进行远程通信时,彼此可以发送各种类型的数据。无论是何种类型的数据,都会以二进制序列的形式在网络上传送。发送方需要把这个对象转换为字节序列,才能在网络上传送;接收方则需要把字节序列再恢复为对象。 把对象转换为字节序列的过程称为对象的序列化。把字节序列恢复为对象的过程称为对象的反序列化。
这里还要注意对象的序列化主要有两种用途:1) 在网络上传送对象的字节序列。2) 把对象的字节序列永久地保存到硬盘上,通常存放在一个文件中。
.net为我们主要提供了三种序列化方式:
它们的主要区别请参考下图:
最后来三段代码,简单实现常用的序列化和反序列化。
1、使用BinaryFormatter
using System;
using System.Data;
using System.Configuration;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary; namespace BinarySerialize
{
//要使一个类可序列化,最简单的方法是使用Serializable 属性对它进行标记
[Serializable]
public class User //要序列化的用户类
{
public int tid = 252;
public string name = "jeff wong";
[NonSerialized]
public string sex = "male"; // 性别被标识为不可序列化
}
public class SerializeUtil
{
private static string strFile = "c:\\User.data"; public static void Serialize(User testUser)
{
using (FileStream fs = new FileStream(strFile, FileMode.Create))
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(fs, testUser);
}
} public static User DeSerialize()
{
User testUser = null;
using (FileStream fs = new FileStream(strFile, FileMode.Open))
{
BinaryFormatter formatter = new BinaryFormatter();
testUser = (User)formatter.Deserialize(fs);
}
return testUser;
}
} public class Program
{
static void Main()
{
SerializeUtil.Serialize(new User());//序列化
User tmpUser = SerializeUtil.DeSerialize();//反序列化
Console.WriteLine(tmpUser.tid + "-" + tmpUser.name);
Console.WriteLine(tmpUser.sex); //sex被标识为不可序列化,所以总为null
Console.ReadLine();
}
}
}
2、使用SoapFormatter
先添加System.Runtime.Serialization.Formatters.Soap引用,然后和BinaryFormatter类似,我们只需要做一下简单修改即可:
a.将using语句中的.Formatter.Binary改为.Formatter.Soap;
b.将所有的BinaryFormatter替换为SoapFormatter.
c.确保报存文件的扩展名为.xml
经过上面简单改动,即可实现SoapFormatter的串行化,这时候产生的文件就是一个xml格式的文件。
using System;
using System.Data;
using System.Configuration;
using System.IO;
using System.Runtime.Serialization.Formatters.Soap; namespace SoapSerialize
{
//要使一个类可序列化,最简单的方法是使用Serializable 属性对它进行标记
[Serializable]
public class User //要序列化的用户类
{
public int tid = 252;
public string name = "jeff wong";
[NonSerialized]
public string sex = "male"; // 性别被标识为不可序列化
}
public class SerializeUtil
{
private static string strFile = "c:\\User.xml"; //存为xml public static void Serialize(User testUser)
{
using (FileStream fs = new FileStream(strFile, FileMode.Create))
{
SoapFormatter formatter = new SoapFormatter();
formatter.Serialize(fs, testUser);
}
} public static User DeSerialize()
{
User testUser = null;
using (FileStream fs = new FileStream(strFile, FileMode.Open))
{
SoapFormatter formatter = new SoapFormatter();
testUser = (User)formatter.Deserialize(fs);
}
return testUser;
}
} public class Program
{
static void Main()
{
SerializeUtil.Serialize(new User());//序列化
User tmpUser = SerializeUtil.DeSerialize();//反序列化
Console.WriteLine(tmpUser.tid + "-" + tmpUser.name);
Console.WriteLine(tmpUser.sex); //sex被标识为不可序列化,所以总为null
Console.ReadLine();
}
}
}
(3)、使用XmlSerializer
想使用XmlSeralizer进行串行化我们需要做一下修改:
a.添加System.Xml.Serialization命名空间.
b.Serializable和NoSerialized属性将被忽略,而是使用XmlIgnore属性,它的行为与NoSerialized类似.
c.XmlSeralizer要求类有个默认的构造器.
using System;
using System.Data;
using System.Configuration;
using System.IO;
using System.Xml.Serialization; namespace XmllSerialize
{
[Serializable]
public class User //要序列化的用户类
{
public int tid = 252;
public string name;
[XmlIgnore]
public string sex = "male";
public User(string name)
{
this.name = name;
}
/// <summary>
/// 必须注意:XmlSeralizer要求类有个默认的构造器
/// </summary>
public User()
{
}
}
public class SerializeUtil
{
private static string strFile = "c:\\User.xml"; //存为xml public static void Serialize(User testUser)
{
XmlSerializer xs = new XmlSerializer(typeof(User));
using (Stream stream = new FileStream(strFile, FileMode.Create, FileAccess.Write, FileShare.Read))
{
xs.Serialize(stream, testUser);
}
} public static User DeSerialize()
{
User testUser = null;
using (FileStream fs = new FileStream(strFile, FileMode.Open, FileAccess.Read, FileShare.Read))
{
XmlSerializer xs = new XmlSerializer(typeof(User));
testUser = (User)xs.Deserialize(fs);
} return testUser;
}
} public class Program
{
static void Main()
{
SerializeUtil.Serialize(new User("jeff wong"));//序列化
User tmpUser = SerializeUtil.DeSerialize();//反序列化
Console.WriteLine(tmpUser.tid + "-" + tmpUser.name);
Console.WriteLine(tmpUser.sex);
Console.ReadLine();
}
}
}
关于常用的XML序列化,可以通过如下通用泛型方法结合扩展方法轻松进行序列化和反序列化操作,代码如下:

using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization; public static class XMLSerializeExtension
{
public static string Serialize<T>(this T value)
{
var result = string.Empty;
if (value == null)
{
return result;
}
try
{
var serializer = new XmlSerializer(typeof(T));
var stringWriter = new StringWriter();
using (var writer = XmlWriter.Create(stringWriter))
{
serializer.Serialize(writer, value);
result = stringWriter.ToString();
}
}
catch (Exception ex)
{
throw new Exception("An error occurred while XML serialize", ex);
} return result;
} public static T Deserialize<T>(this string value)
{
T retObj = default(T);
if (string.IsNullOrWhiteSpace(value) == true)
{
return retObj;
}
try
{
using (var rdr = new StringReader(value))
{
var serializer = new XmlSerializer(typeof(T));
retObj = (T)serializer.Deserialize(rdr);
}
}
catch (Exception ex)
{
throw new Exception("An error occurred while XML Deserialize", ex);
} return retObj;
} }

必须注意,XmlSerializer要慎用, 容易引起动态加载dll引发内存泄漏,这里是内存泄漏的例子。
c#:浅克隆和深克隆,序列化和反序列化的更多相关文章
- java浅克隆和深克隆,序列化和反序列化实现深克隆(封装序列化和反序列化操作)
本篇博客内容: 一.浅克隆(ShallowClone)和深克隆(DeepClone) 二.序列化和反序列化实现深克隆 三.封装序列化和反序列化操作 ObjectOutputStream + 内存流By ...
- 【Java一看就懂】浅克隆和深克隆
一.何为克隆 在Java的体系中,数据类型分为基本数据类型和引用数据类型. 基本数据类型包括byte,short,int,long,float,double,boolean,char 8种,其克隆可通 ...
- Java的浅克隆和深克隆
如何实现对象的克隆 (1)实现 Cloneable 接口并重写 Object 类中的 clone() 方法: (2)实现 Serializable 接口,通过对象的序列化和反序列化实现克隆,可以实现真 ...
- Java对象的浅克隆和深克隆
为什么需要克隆 在实际编程过程中,我们常常要遇到这种情况:有一个对象A,在某一时刻A中已经包含了一些有效值,此时可能会需要一个和A完全相同新对象B, 并且此后对B任何改动都不会影响到A中的值 ...
- Java必备技能:clone浅克隆与深克隆
介绍 一直以来只知道Java有clone方法,该方法属于Object的,对于什么是浅克隆与深克隆就比较模糊了,现在就来补充学习一下. 概念 浅拷贝(浅克隆)复制出来的对象的所有变量都含有与原来的对象相 ...
- java序列化和反序列化使用总结
一.概念 java对象序列化的意思就是将对象的状态转化成字节流,以后可以通过这些值再生成相同状态的对象.对象序列化是对象持久化的一种实现方法,它是将对象的属性和方法转化为一种序列化的形式用于存储和传输 ...
- C# 序列化与反序列化几种格式的转换
这里介绍了几种方式之间的序列化与反序列化之间的转换 首先介绍的如何序列化,将object对象序列化常见的两种方式即string和xml对象; 第一种将object转换为string对象,这种比较简单没 ...
- 使用Newtonsoft.Json.dll(JSON.NET)动态解析JSON、.net 的json的序列化与反序列化(一)
在开发中,我非常喜欢动态语言和匿名对象带来的方便,JSON.NET具有动态序列化和反序列化任意JSON内容的能力,不必将它映射到具体的强类型对象,它可以处理不确定的类型(集合.字典.动态对象和匿名对象 ...
- Java 序列化与反序列化
1.什么是序列化?为什么要序列化? Java 序列化就是指将对象转换为字节序列的过程,而反序列化则是只将字节序列转换成目标对象的过程. 我们都知道,在进行浏览器访问的时候,我们看到的文本.图片.音频. ...
随机推荐
- 【POJ】3678 Katu Puzzle
http://poj.org/problem?id=3678 题意:很幼稚的题目直接看英文题面= = #include <cstdio> #include <cstring> ...
- 【noiOJ】p1776
t1776:木材加工 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 木材厂有一些原木,现在想把这些木头切割成一些长度相同的小段木头,需要得到的小段的数目是给 ...
- java画图程序_图片用字母画出来
最近在研究怎样将图片用字母在文本编辑工具中“画”出来. 你看了这个可能还不知道我想说什么? 我想直接上图,大家一定就知道了 第一张:小猫 原图:http://www.cnblogs.com/hongt ...
- 类库,委托,is和as运算符,泛型集合
类库:其实就是一堆类文件,只不过用户看不到这些类的源代码,保密性好. 优点:保密性好缺点:如果这个方法不好用,使用者无法自己去更改它. 类文件是.cs 类库是.dll 新建项目为类库,在debu ...
- thinkphp 的create()非法数据解决办法
是因为create()方法默认是使用post传值的,把from表单的传值方法改成post就行了,默认是get.
- CSS DIV 独占一行,清除左右两边的浮动
clear:both //表示清除左右两边浮动的层,自己独占一行
- PHP filesystem attack vectors
http://www.ush.it/2009/02/08/php-filesystem-attack-vectors/ On Apr 07, 2008 I spoke with Kuza55 and ...
- FTP目录或文件名有中文时导致,下载失败的问题
在FTPClient ftpClient = new FTPClient()代码后, ftpClient.connect(url,port)代码前, 调用ftpClient.setAutodetect ...
- JavaScript入门篇 第二天(消息对话框+网页弹出)
提问(prompt 消息对话框) prompt弹出消息对话框,通常用于询问一些需要与用户交互的信息.弹出消息对话框(包含一个确定按钮.取消按钮与一个文本输入框). 语法: prompt(str1, s ...
- javaweb--HTTP状态码
HTTP状态码(HTTP Status Code) 一些常见的状态码为: 200 - 服务器成功返回网页 404 - 请求的网页不存在 503 - 服务不可用 所有状态解释:点击查看 1xx(临时响应 ...