.NET 对象的序列化和反序列化

 DataSet ds = new DataSet();
            //给ds赋值(省略)
            byte[] b = this.Serialize(ds);
            DataSet d1 = this.DeSerialize(b) as DataSet;
            String s = "voodooer";
            byte[] b1 = this.Serialize(s);
            String s1 = this.DeSerialize(b1) as Stirng;


/// <summary>
        /// 序列化对象
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public byte[] Serialize(object obj)
        {
            MemoryStream ms = new MemoryStream();
            BinaryFormatter b = new BinaryFormatter();
            b.Serialize(ms, obj);
            return ms.ToArray();
        }
        /// <summary>
        /// 反序列化对象
        /// </summary>
        /// <param name="bt"></param>
        /// <returns></returns>
        public object DeSerialize(byte[] bt)
        {
            MemoryStream ms = new MemoryStream(bt);
            BinaryFormatter b = new BinaryFormatter();
            return b.Deserialize(ms);
        }

.NET 对象的序列化和反序列化,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。