对象序列化与反序列化(二进制 byte[])


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 = 0; byte[] bytes = new byte[ms.Length]; ms.Read(bytes, 0, bytes.Length); ms.Close(); return bytes; } string objectString=System.Convert.ToBase64String(SerializeObject(importedObj)); 2.反序列化 public static object DeserializeObject(byte[] bytes) { object obj = null; if (bytes == null) return obj; MemoryStream ms = new MemoryStream(bytes); ms.Position = 0; BinaryFormatter formatter = new BinaryFormatter(); obj = formatter.Deserialize(ms); ms.Close(); return obj; }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM