当前位置:   article > 正文

C# 类的序列化和反序列化_ly.mds.interface.service.basedata.icartype__system

ly.mds.interface.service.basedata.icartype__system.runtime.serialization.ise

System.Runtime.Serialization.SerializationException”类型的未经处理的异常在 mscorlib.dll 中发生 

 

序列化 (Serialization)将对象的状态信息转换为可以存储或传输的形式的过程。在序列化期间,对象将其当前状态写入到临时或持久性存储区。以后,可以通过从存储区中读取或反序列化对象的状态,重新创建该对象。(摘自百度百科)

   在很多通讯或者数据存储的过程中,都需要序列化和反序列化的过程。

 

  在C#中,如果想要进行自定义类的序列化,只需要简单地在定义类的时候添加Serializable标签即可。如:

1

2

3

4

5

6

[Serializable]

public class Person

{

    public string name;

    public int age;

}

  经常用到的序列化方式有两种:二进制和XML,分为由System.Runtime.Serialization.Formatters.Binary.BinaryFormatter和System.Xml.Serialization.XmlSerializer实现。

  BinaryFormatter进行序列化和反序列化的时候,不会关心实际类型。但是XmlSerializer需要。

  

  自己写的一个简单的序列化辅助类

  

复制代码

    public static class SerializeHelper
    {
        /// <summary>
        /// 使用UTF8编码将byte数组转成字符串
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string ConvertToString(byte[] data)
        {
            return Encoding.UTF8.GetString(data , 0 , data.Length);
        }

        /// <summary>
        /// 使用指定字符编码将byte数组转成字符串
        /// </summary>
        /// <param name="data"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static string ConvertToString(byte[] data , Encoding encoding)
        {
            return encoding.GetString(data , 0 , data.Length);
        }

        /// <summary>
        /// 使用UTF8编码将字符串转成byte数组
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static byte[] ConvertToByte(string str)
        {
            return Encoding.UTF8.GetBytes(str);
        }

        /// <summary>
        /// 使用指定字符编码将字符串转成byte数组
        /// </summary>
        /// <param name="str"></param>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public static byte[] ConvertToByte(string str , Encoding encoding)
        {
            return encoding.GetBytes(str);
        }

        /// <summary>
        /// 将对象序列化为二进制数据 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static byte[] SerializeToBinary(object obj)
        {
            MemoryStream stream = new MemoryStream( );
            BinaryFormatter bf = new BinaryFormatter( );
            bf.Serialize(stream , obj);

            byte[] data = stream.ToArray( );
            stream.Close( );

            return data;
        }

        /// <summary>
        /// 将对象序列化为XML数据
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static byte[] SerializeToXml(object obj)
        {
            MemoryStream stream = new MemoryStream( );
            XmlSerializer xs = new XmlSerializer(obj.GetType( ));
            xs.Serialize(stream , obj);

            byte[] data = stream.ToArray( );
            stream.Close( );

            return data;
        }

        /// <summary>
        /// 将二进制数据反序列化
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static object DeserializeWithBinary(byte[] data)
        {
            MemoryStream stream = new MemoryStream( );
            stream.Write(data , 0 , data.Length);
            stream.Position = 0;
            BinaryFormatter bf = new BinaryFormatter( );
            object obj = bf.Deserialize(stream);

            stream.Close( );

            return obj;
        }

        /// <summary>
        /// 将二进制数据反序列化为指定类型对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <returns></returns>
        public static T DeserializeWithBinary<T>(byte[] data)
        {
            return (T)DeserializeWithBinary(data);
        }

        /// <summary>
        /// 将XML数据反序列化为指定类型对象
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <returns></returns>
        public static T DeserializeWithXml<T>(byte[] data)
        {
            MemoryStream stream = new MemoryStream( );
            stream.Write(data , 0 , data.Length);
            stream.Position = 0;
            XmlSerializer xs = new XmlSerializer(typeof(T));
            object obj = xs.Deserialize(stream);

            stream.Close( );

            return (T)obj;
        }
    }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/111854
推荐阅读
相关标签
  

闽ICP备14008679号