![对象序列化与反序列化(二进制 byte[]) 对象序列化与反序列化(二进制 byte[])](https://image.shishitao.com:8440/aHR0cHM6Ly9ia3FzaW1nLmlrYWZhbi5jb20vdXBsb2FkL2NoYXRncHQtcy5wbmc%2FIQ%3D%3D.png?!?w=700&webp=1)
.序列化
public static byte[] SerializeObject(object obj)
{
if (obj == null)
return null;
MemoryStream ms = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(ms, obj);
ms.Position = ;
byte[] bytes = new byte[ms.Length];
ms.Read(bytes, , bytes.Length);
ms.Close();
return bytes;
} string objectString=System.Convert.ToBase64String(SerializeObject(importedObj)); .反序列化
public static object DeserializeObject(byte[] bytes)
{
object obj = null;
if (bytes == null)
return obj;
MemoryStream ms = new MemoryStream(bytes);
ms.Position = ;
BinaryFormatter formatter = new BinaryFormatter();
obj = formatter.Deserialize(ms);
ms.Close();
return obj;
}