序列化数据方法:
/// <summary>
/// 序列化数据
/// </summary>
/// <param name="objData">需要序列化的对象</param>
/// <returns></returns>
public static byte[] SerializeData(object objData)
{
MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, objData);
ms.Close();
return ms.ToArray();
}
反序列化方法:
/// <summary>
/// 反序列化
/// </summary>
/// <param name="data">已序列化的数据</param>
/// <returns></returns>
public static object DeserializeData(byte[] data)
{
MemoryStream ms = new MemoryStream(data, 0, data.Length);
BinaryFormatter bf = new BinaryFormatter();
return bf.Deserialize(ms);
}
以上方法需引入命名空间:
using System.Runtime.Serialization.Formatters.Binary;