序列化与反序列化代码

来源:互联网 发布:windows查看cpu命令 编辑:程序博客网 时间:2024/05/11 20:07
        /**/        ///*****************************************        /// <summary>        /// 序列化一个对象        /// </summary>        /// <param name="o">将要序列化的对象</param>        /// <returns>返回byte[]</returns>        ///*****************************************        public static byte[] Serialize(object o)        {            if (o == null) return null;            BinaryFormatter formatter = new BinaryFormatter();            MemoryStream ms = new MemoryStream();            formatter.Serialize(ms, o);            ms.Position = 0;            byte[] b = new byte[ms.Length];            ms.Read(b, 0, b.Length);            ms.Close();            return b;        }        /**/        ///*****************************************        /// <summary>        /// 反序列化        /// </summary>        /// <param name="b">返回一个对象</param>        ///*****************************************        public static object Deserialize(byte[] b)        {            if (b.Length == 0) return null;            try            {                BinaryFormatter bf = new BinaryFormatter();                MemoryStream ms = new MemoryStream();                ms.Write(b, 0, b.Length);                ms.Position = 0;                object n = (object)bf.Deserialize(ms);                ms.Close();                return n;            }            catch (Exception e)            {                System.Diagnostics.Debug.WriteLine(e.ToString());                return null;            }        }

原创粉丝点击