当前位置: 编程技术>.net/c#/asp.net
c#泛型序列化对象为字节数组的示例
来源: 互联网 发布时间:2014-10-30
本文导语: 序列化对象为字节数组 代码如下:using System.IO;using System.Runtime.Serialization.Formatters.Binary; protected byte[] Serialize(T t) { MemoryStream mStream = new MemoryStream(); BinaryFormatter bFormatter = new Bi...
序列化对象为字节数组
代码如下:
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
protected byte[] Serialize(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(byte[] b)
{
BinaryFormatter bFormatter = new BinaryFormatter();
return (T)bFormatter.Deserialize(new MemoryStream(b));
}