首页 > 其他 > 详细

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

时间:2014-01-24 08:05:42      阅读:496      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
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;
        }
bubuko.com,布布扣

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

原文:http://www.cnblogs.com/quietwalk/p/3531712.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!