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));
}