zoukankan      html  css  js  c++  java
  • c#中序列化和反序列化的理解

    using System.IO;
    using System.Runtime.Serialization.Formatters.Binary;

    序列化:对象转化为文件的过程(字节流)

    反序列化:文件(字节流)转化为对象的过程

            private string _Path = @"D:LearningSerilizable.txt";
    
            /// <summary>
            /// 进行序列化
            /// </summary>
            public void Test1()
            {
                Person p = new Person("children", 19);
    
                if (File.Exists(_Path))
                {
                    using (FileStream fs = new FileStream(_Path, FileMode.OpenOrCreate))
                    {
                        BinaryFormatter bf = new BinaryFormatter();
                        bf.Serialize(fs, p);
                    }
                }   
            }

    对一个对象序列化过程:

    1:对象应该是可以被序列化[Serializable]

    2:序列化过程还是需要文件流,定义文件流,确定路径。

    3:使用BinaryFormatter进行序列化和反序列化。

    还有梦的时候就别轻易选择放弃,追求梦想的过程你真的很美!

  • 相关阅读:
    使用合理jQuery选择器查找DOM元素
    DOM对象和jQuery对象
    jQuery实现返回顶部
    行内元素,块级元素
    图片自适应缩放
    幽灵按钮
    background-attachment:fixed
    RegExp
    正则
    Date
  • 原文地址:https://www.cnblogs.com/Optimism/p/10452717.html
Copyright © 2011-2022 走看看