用于深拷贝的扩展方法 C#

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

    public static class Tool
    {
        public static T DeepCopy<T>(this T obj)
        {
            BinaryFormatter formatter = new BinaryFormatter();
            Stream stream=new MemoryStream();
            formatter.Serialize(stream, obj);
            stream.Position = 0;
            T returnObj = (T)formatter.Deserialize(stream);
            return returnObj;
        }
    }

 

posted on 2017-12-28 13:43  方辰  阅读(134)  评论(0编辑  收藏  举报

导航