• C# 序列化的方法


    由于表达能力太差,直接上代码..

     List<Serializationcs.Student> list = new List<Serializationcs.Student>();
                    list.Add(new Serializationcs.Student
                    {
                        Age=12,Name="test",No="1232"
                   

                    });
                    list.Add(new Serializationcs.Student
                    {

                        Age = 34,
                        Name = "testtest",
                        No = "444"
                    });


                    Serializationcs serial = new Serializationcs();
                    serial.BinaryFormater(list);

                    serial.BinaryDESFormater();

     public class Serializationcs
        {


            /// 序列化一个对象的状态持久化,把对象的信息保存到一些存储介质中,
            /// 在需要时反序列化获取该对象的信息。主要应用需要序列化对象更能方便在在服务端和客户端之间传输///
            ///  序列化已知有三种一.二进制,二XML序列化,三是SOAP序列化常用的是前两种.
            ///  
            //反序列化

            /// <summary>
            /// 二进制的反序列化
            /// </summary>
            public void BinaryDESFormater()
            {
                using (FileStream fs = new FileStream("test.bin", FileMode.Open))
                {
                    BinaryFormatter bf = new BinaryFormatter();
                    List<Student> list = bf.Deserialize(fs) as List<Student>;
                    if (list != null)
                    {

                        for (int i = 0; i < list.Count; i++)
                        {
                            list[i].Say();
                        }
                    }

                }
            }
            /// <summary>
            /// 二进制的序列化
            /// </summary>
            /// <param name="list"></param>
            public void BinaryFormater(List<Student> list)
            {
                    //序列化
                    using (FileStream fs = new FileStream("test.bin", FileMode.Create))
                    {
                        BinaryFormatter bf = new BinaryFormatter();
                        bf.Serialize(fs, list);
                    }
                

            }

            public void XmlFormatter(List<Student> list)
            {

                XmlSerializer xs = new XmlSerializer(typeof(List<Student>));
                using (Stream fs = new FileStream("xml.bat", FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    xs.Serialize(fs, list);
                }
            }
            public void XmlDesFormatter()
            {

                XmlSerializer xs = new XmlSerializer(typeof(List<Student>));
                using (Stream fs = new FileStream("xml.bat", FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    List<Student> list = xs.Deserialize(fs) as List<Student>;
                    if (list != null)
                    {

                        for (int i = 0; i < list.Count; i++)
                        {
                            list[i].Say();
                        }
                    }
                }
            }

     
            [Serializable]
            public class Student
            {
                private string _name;
                private string _no;
                [NonSerialized]///可以排除序列化
                private int _age;


                public string Name
                {
                    get { return _name; }
                    set { _name = value; }

                }
                public string No
                {
                    get { return _no; }
                    set { _no = value; }
                }
              
                public int Age
                {
                    get { return _age; }
                    set { _age = value; }
                }

                public void Say()
                {
                    Console.Write(Name, No);
                }
            }

        }

  • 相关阅读:
    js html5 绘制折线图
    软件资源下载链接
    二维vector容器读取txt坐标
    C++ opencv 滑动条 Trackbary以及处理三通道和单通道图像
    C++文件读写函数之——fopen、fread和fwrite、fgetc和fputc、fgets和fputs、ftellf和fseek、rewind
    opencv-将分离合并图像(Red通道>125置255<=置0)
    C++ vector类型要点总结(以及各种algorithm算法函数)
    C++容器嵌套实现动态二维数组(Vector2D)
    用vector实现二维向量
    C++标准库vector及迭代器
  • 原文地址:https://www.cnblogs.com/cxlings/p/2784552.html
Copyright © 2020-2023  润新知