zoukankan      html  css  js  c++  java
  • 二进制流BinaryFormatter存储读取数据的细节测试

    二进制流的使用很方便,为了更好的理解应用它,我创建简单对象开始测试它的增加特性和减少特性.

            [Serializable]
            class Data----------开始时候的存储对象
            {
                public string name = "namex";
                public string ceshia = "aa";
                public string ceshib = "bb";
            }
    
            [Serializable]
            class Data----------在代码中修改存储类, 增加了新的字段
            {
                public string name = "namex";
                public string ceshia = "aa";
                public string ceshib = "bb";
    
                public string ceshic = "cc";
                public int md = 21;
                public long ml = 2;
            }
    
    //读取后,新增加的值由于在原来存储文件中不存在,它们被赋予默认初始值,而非代码中指定的初始值.
    
                string ceshic = null;
                int md = 0;
                long ml = 0;
    
    //减少数据后, 不会影响原来的值.存储多余的值被自动忽略
    
            [Serializable]
            class Data----------在代码中修改存储类, 
            {
                public string name = "namex";
                public string ceshia = "aa";
                public string ceshib = "bb";
    
               // public string ceshic = "cc";
               // public int md = 21;
               // public long ml = 2;
            }
    
    //需要注意的点来了,新增加的变量名字不能原来存储过的变量名字相同,如果相同,系统不会因为变量类型不同,而判断是不同变量值,而会抛出异常,提醒你无法转化.
            [Serializable]
            class Data----------在代码中修改存储类, 
            {
                public string name = "namex";
                public string ceshia = "aa";
                public string ceshib = "bb";
    
               // public string ceshic = "cc";
               // public int md = 21;
               // public long ml = 2;
                   public string ml = 2;//新增加的变量名字和原先存储的数据变量名字相同
            }

    所以在多次增加或者删除字段时,尽量保持变量的名字都是不同的,这样增减字段都会保持与过去存储文件的兼容性!

  • 相关阅读:
    Python 学习日记 第七天
    Python 学习日记 第六天
    Python 学习日记 第五天
    Python 学习日记 第四天
    Redis 中的数据类型及基本操作
    Asp.net mvc 中View 的呈现(二)
    Asp.net mvc 中View的呈现(一)
    Asp.net mvc 中Action 方法的执行(三)
    Asp.net mvc 中Action 方法的执行(二)
    Asp.net mvc 中Action 方法的执行(一)
  • 原文地址:https://www.cnblogs.com/flyant/p/4413358.html
Copyright © 2011-2022 走看看