[转载]通用的序列化方法 – 邹泽栋 – 博客园.
04 |
/// <param name="obj">对象</param> |
05 |
/// <returns>二进制流</returns> |
06 |
private byte [] GetBinaryFormatSerialize( object obj) |
09 |
MemoryStream Memory = new MemoryStream(); |
11 |
BinaryFormatter formatter = new BinaryFormatter(); |
13 |
formatter.Serialize(Memory, obj); |
14 |
byte [] buff = Memory.ToArray(); |
23 |
/// <typeparam name="T">类型</typeparam> |
24 |
/// <param name="buff">二进制数据</param> |
25 |
/// <returns>反序列后结果</returns> |
26 |
private T RetrieveDeserialize<T>( byte [] buff) |
29 |
MemoryStream Memory = new MemoryStream(); |
31 |
BinaryFormatter formatter = new BinaryFormatter(); |
32 |
return (T)formatter.Deserialize( new MemoryStream(buff)); |
34 |
<pre class = "brush:csharp" > |
35 |
DataTable dt = new DataTable(); |
37 |
byte [] buff = GetBinaryFormatSerialize(dt); |
41 |
dt = RetrieveDeserialize<DataTable>(buff); |