您的位置:首页 > 编程语言 > C#

C#深度拷贝,浅拷贝

2017-07-17 23:14 211 查看

使用序列化的方法实现深度拷贝非常方便

using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

[Serializable]
class Person : ICloneable
{
public object Clone()
{
return this.MemberwiseClone();
}

public Person DeepClone()
{
using(Stream os = new MemoryStream())
{
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(os, this);
os.Seek(0, SeekOrigin.Begin);
return formatter.Deserialize(os) as Person;
}
}

public Person ShallowClone()
{
return Clone() as Person;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c#