正确实现浅拷贝与深拷贝

正确实现浅拷贝与深拷贝_第1张图片

using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
[Serializable]
public class Employee : ICloneable
{
    public string IDCode { get; set; }
    public int Age { get; set; }

    public object Clone()
    {
        return this.MemberwiseClone();
    }

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

    public Employee ShallowClone()
    {
        return Clone() as Employee;
    }
}

 

你可能感兴趣的:(C#,C#,拷贝,深拷贝,浅拷贝)