c#泛型序列化對象為字節(jié)數(shù)組的示例
更新時間:2014年04月22日 10:43:29 作者:
這篇文章主要介紹了c#泛型序列化對象為字節(jié)數(shù)組的示例,需要的朋友可以參考下
序列化對象為字節(jié)數(shù)組
復(fù)制代碼 代碼如下:
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();
}
反序列化字節(jié)數(shù)組為對象
復(fù)制代碼 代碼如下:
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));
}
相關(guān)文章
深入C# 4.0 新特性dynamic、可選參數(shù)、命名參數(shù)的詳細介紹
本篇文章是對C# 4.0 新特性dynamic、可選參數(shù)、命名參數(shù)進行了詳細的分析介紹,需要的朋友參考下2013-05-05C#組件系列 你值得擁有的一款Excel處理神器Spire.XLS
又一款Excel處理神器Spire.XLS,這篇文章主要為大家詳細介紹了第三方組件Spire.XLS,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09