zoukankan      html  css  js  c++  java
  • 序列化- 使用BinaryFormatter进行序列化

    可以使用属性(Attribute)将类的元素标为可序列化的(Serializable)和不可被序列化的(NonSerialized)。.NET中有两个类实现了IFormatter借口的类中的Serialize和Deserialize方法:BinaryFormatter和SoapFormatter。这两个类的区别在于数据流的格式不同。

    使用BinaryFormatter进行序列化
    在下面这个例子中我们建立一个自定义类型(Insect)集合,使用BinaryFormatter将它们写到二进制文件,然后再将他们读回。
    注:以下程序需要导入一些命名空间:
    using System;
    using System.IO;
    using System.Collections;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters.Binary;

    [Serializable]
    public class Insect
    {
        private string name;
        
        [NonSerialized]
        private int id;
        
        public Insect(string name, int id)
        {
            this.name = name;
            this.id= id;
        }
        public override string ToString()
        {
            return String.Format("{0}:{1}", name, id);
        }
    }

    我们使用一个标准属性将整个Insect类声明为可序列化的。但是因为一个字段被声明为不可序列化,所以这个字段不能被持久化。

    我们先做一个试验,我们只实例化一个Insect对象,创建一个文件,然后使用BinaryFormatter对象和Serialize方法写出这个Insect对象:

    class SerializeApp
    {
        public static void  Main(string[] args)
        {
            Insect i = new Insect("Meadow Brown", 12);
            Stream sw = File.Create("Insects.bin");
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(sw, i);
            sw.Close();
            }
    }

    如果在Visual Studio打开Insect.bin文件就会看到以下内容:
    FBinaryFormatter, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null Insect name Meadow Brown(由于我没有截图软件,所以这只是部分内容)
    我们可以注意到并没有id字段,因为它没有被序列化。

    现在,我们增加几个Insect对象。

            ArrayList box = new ArrayList();
            box.Add(new Insect("Marsh Fritillary", 34));
            box.Add(new Insect("Speckled Wood", 56));
            box.Add(new Insect("Milkweed", 78));
            sw = File.Open("Insects.bin", FileMode.Append);
            bf.Serialize(sw, box);
            sw.Close();
            
            Stream sr = File.OpenRead("Insects.bin");
            Insect j = (Insect)bf.Deserialize(sr);
            Console.WriteLine(j);
            
            ArrayList bag = (ArrayList)bf.Deserialize(sr);
            sr.Close();
            foreach(Insect k in bag)
            {
                Console.WriteLine(k);
            }


    下面是这个程序的输出:
    Meadow Brown:0
    Marsh Fritillary:0
    Speckled Wood:0
    Milkweed:0

    id值是0,其原因是很明显的(它在foreach循环中构造Insect的期间被初始化为0)。
    注意,我们非常小心地先读回一个Insect对象 - 在读回集合之前已经被序列化到文件的对象。
    另外,在我们使用Deserialize时,必须对返回的对象进行类型转换,因为这个方法返回一个一般性的对象。

    在后面添加的集合中有三个Insect的数据,这节省了一些开销,因为只需要为第一列的Insect记录Insect类的类型信息。
    另外一个有意思的地方是,序列化机制显然能够读写列中的私有字段。

  • 相关阅读:
    多媒体笔记
    全部MIME类型列表
    PHP 标记 <?= <?php
    js渐显文字 时间间隔
    ThinkPHP5 隐式传参的获取方法
    [php] Fatal error: Class Exception not found in
    ionic2 The --v1 and --v2 flags have been removed.
    java POI导出excel,合并单元格边框消失
    swagger暴露程序接口文档
    SpringBoot整合Mybatis
  • 原文地址:https://www.cnblogs.com/gjhjoy/p/3490231.html
Copyright © 2011-2022 走看看