zoukankan      html  css  js  c++  java
  • C#序列化

    1.序列化一般有2种(XML和2进制),简单对象序列化

    using System;
    using System.IO;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Windows.Forms;
    using System.Xml.Serialization;
    
    namespace Test
    {
        //对于XmlSerializer序列化,默认即使不使用特性Serializable,也是可以对对象进行序列化的,则BinaryFormatter不然一定要使用Serializable标记。
        public partial class Form1 : Form
        {
            //XmlSerializer是XML序列化
            XmlSerializer xs = new XmlSerializer(typeof(Student));
            //二进制序列化
            BinaryFormatter b = new BinaryFormatter();
            Student student = new Student() { Name = "小明", Age = 15 };
            public Form1()
            {
                InitializeComponent();
                //xml序列化
                using (Stream stream = new FileStream("d:\Student.xml", FileMode.Create, FileAccess.Write, FileShare.Read))
                {
                    xs.Serialize(stream, student);
                }
                //xml反序列化
                using (FileStream fs = new FileStream("d:\Student.xml", FileMode.Open, FileAccess.Read))
                {
                    Student student = (Student)xs.Deserialize(fs);
                }
                //二进制序序列化
                using (FileStream fileStream = new FileStream("d:\Student.dat", FileMode.Create))
                {
                    BinaryFormatter b = new BinaryFormatter();
                    //序列化类要加[Serializable]特性
                    b.Serialize(fileStream, student);
                }
                //二进制序反序列化
                using (FileStream fileStream = new FileStream("d:\Student.dat", FileMode.Open, FileAccess.Read))
                {
                    BinaryFormatter bf = new BinaryFormatter();
                    student = (Student)bf.Deserialize(fileStream);
                }
            }
    
        }
    }
    
    [Serializable]
    public class Student
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }

     2.复杂对象序列化

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Xml.Serialization;
    
    namespace Test
    {
        public partial class Form1 : Form
        {
            //XmlSerializer是XML序列化
            XmlSerializer xs = new XmlSerializer(typeof(TeacherStudent));
            //二进制序列化
            BinaryFormatter b = new BinaryFormatter();
            TeacherStudent teacherStudent = new TeacherStudent();
    
            Teacher teacher = new Teacher() { Name = "王老师", Age = 45 };
            Student student = new Student() { Name = "小明", Age = 15 };
            public Form1()
            {
                InitializeComponent();
                //xml序列化
                using (Stream stream = new FileStream("d:\Student.xml", FileMode.Create, FileAccess.Write, FileShare.Read))
                {
                    teacherStudent.Teacher = teacher;
                    teacherStudent.Student = student;
                    xs.Serialize(stream, teacherStudent);
                }
                //xml反序列化
                using (FileStream fs = new FileStream("d:\Student.xml", FileMode.Open, FileAccess.Read))
                {
                    teacherStudent = null;
                    teacherStudent = (TeacherStudent)xs.Deserialize(fs);
                }
                //二进制序序列化
                using (FileStream fileStream = new FileStream("d:\Student.dat", FileMode.Create))
                {
                    BinaryFormatter b = new BinaryFormatter();
                    b.Serialize(fileStream, teacher);
                    b.Serialize(fileStream, student);
                }
                //二进制序反序列化
                using (FileStream fileStream = new FileStream("d:\Student.dat", FileMode.Open, FileAccess.Read))
                {
                    teacher = null;
                    student = null;
                    BinaryFormatter bf = new BinaryFormatter();
                    teacher = (Teacher)bf.Deserialize(fileStream);
                    student = (Student)bf.Deserialize(fileStream);
                }
            }
    
        }
    }
    
    [Serializable]
    public class Student
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
    
    
    [Serializable]
    public class Teacher
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
    
    [Serializable]
    public class TeacherStudent
    {
        public Teacher Teacher { get; set; }
        public Student Student { get; set; }
    }

    3. 控制序列化/反序列化前后的数据

    using System;
    using System.IO;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Windows.Forms;
    
    namespace Test
    {
        public partial class Form1 : Form
        {
            //XmlSerializer是XML序列化
            BinaryFormatter b = new BinaryFormatter();
            Student student = new Student() { Name = "小明", Age = 15 };
            public Form1()
            {
                InitializeComponent();
                //二进制序序列化
                using (FileStream fileStream = new FileStream("d:\Student.dat", FileMode.Create))
                {
                    BinaryFormatter b = new BinaryFormatter();
                    b.Serialize(fileStream, student);
                }
                //二进制序反序列化
                using (FileStream fileStream = new FileStream("d:\Student.dat", FileMode.Open, FileAccess.Read))
                {
                    BinaryFormatter bf = new BinaryFormatter();
                    student = (Student)bf.Deserialize(fileStream);
                }
            }
    
        }
    }
    
    [Serializable]
    public class Student
    {
        public string Name { get; set; }
        public int Age { get; set; }
    
        [OnSerializing()]
        internal void OnSerializingMethod(StreamingContext context)
        {
            //格式化器在序列化开始之前调用此方法。
            Console.WriteLine("OnSerializing格式化器在序列化开始之前调用此方法");
        }
    
        [OnSerialized()]
        internal void OnSerializedMethod(StreamingContext context)
        {
            //格式化器在序列化后调用此方法。
            Console.WriteLine("OnSerialized格式化器在序列化后调用此方法");
        }
    
        [OnDeserializing()]
        internal void OnDeserializingMethod(StreamingContext context)
        {
            //格式化器在反序列化开始之前调用此方法。
            Console.WriteLine("OnDeserializing格式化器在反序列化开始之前调用此方法");
        }
    
        [OnDeserialized()]
        internal void OnDeserializedMethod(StreamingContext context)
        {
            //格式化器在序列化开始之前调用此方法。
            Console.WriteLine("OnDeserialized格式化器在序列化开始之前调用此方法");
        }
    }
  • 相关阅读:
    swagger序列化对example属性的特殊处理造成的json格式异常问题
    Elasticsearch 6.2.4 xpack白金版破解-仅供交流学习使用
    Logback多进程写入同一日志文件时滚动日期混乱问题
    mycat事务中上来执行select马上提交——小猫如此折腾,我选dble
    我家很管事的猫——mycat初步部署实践与问题排查
    certbot https签发证书与自动更新——acme实在太难用,certbot一键式全搞定
    自力更生Collections.sort发现比较结果混乱?Comparator的锅还是强转类型导致?
    Java SPI、servlet3.0与@HandlesTypes源码分析
    真——Springcloud支持Https
    Controller层的方法访问标志与Spring装配与AspectJ切面处理
  • 原文地址:https://www.cnblogs.com/lgxlsm/p/5860812.html
Copyright © 2011-2022 走看看