c# 内存拷贝 解决json序列化丢失私有数据(二进制序列化反序列化)

public static class CopyEx
{
public static T DeepCopyByBin( T obj)
{
object retval;
using (MemoryStream ms = new MemoryStream())
{
BinaryFormatter bf = new BinaryFormatter();
//序列化成流
bf.Serialize(ms, obj);
ms.Seek(0, SeekOrigin.Begin);
//反序列化成对象
retval = bf.Deserialize(ms);
ms.Close();
}
return (T)retval;
}
}

posted @ 2021-01-07 20:23  Hey,Coder!  阅读(383)  评论(0编辑  收藏  举报