C# 对象转换为byte[] ,byte[]还原对象


/// 

 
/// 将一个object对象序列化,返回一个byte[]         
/// 
 
/// 能序列化的对象         
///  
public static byte[] ObjectToBytes(object obj)
{
using (MemoryStream ms = new MemoryStream())
{
IFormatter formatter = new BinaryFormatter(); formatter.Serialize(ms, obj); return ms.GetBuffer();
}
}

/// 

 
/// 将一个序列化后的byte[]数组还原         
/// 

///          
///  
public static object BytesToObject(byte[] Bytes)
{
using (MemoryStream ms = new MemoryStream(Bytes))
{
IFormatter formatter = new BinaryFormatter(); return formatter.Deserialize(ms);
}
}

C