C# 通用Clone

浅拷贝:可以直接调用this.MemberwiseClone()完成

深拷贝:

Clone基类:

    [Serializable]
    class BaseClone<T>
    {
        public virtual T Clone()
        {
            MemoryStream memoryStream = new MemoryStream();
            BinaryFormatter formatter = new BinaryFormatter();
            formatter.Serialize(memoryStream, this);
            memoryStream.Position = 0;
            return (T)formatter.Deserialize(memoryStream);
            
        }
    }

需要Clone的类加上[Serializable]并且继承BaseClone<T>直接调用Clone方法就可以完成深拷贝

原文地址:https://www.cnblogs.com/FlyCat/p/2703064.html