C#教程

关注公众号 jb51net

关闭
首页 > 软件编程 > C#教程 > c#泛型序列化

c#泛型序列化对象为字节数组的示例

作者:

这篇文章主要介绍了c#泛型序列化对象为字节数组的示例,需要的朋友可以参考下

序列化对象为字节数组

复制代码 代码如下:

using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
        protected byte[] Serialize<T>(T t)
        {
            MemoryStream mStream = new MemoryStream();
            BinaryFormatter bFormatter = new BinaryFormatter();
            bFormatter.Serialize(mStream, t);
            return mStream.GetBuffer();
        }

反序列化字节数组为对象

复制代码 代码如下:

using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
        protected T Deserialize<T>(byte[] b)
        {
            BinaryFormatter bFormatter = new BinaryFormatter();
            return (T)bFormatter.Deserialize(new MemoryStream(b));
        }

您可能感兴趣的文章:
阅读全文