zoukankan      html  css  js  c++  java
  • 序列化与反序列化

    序列化与反序列化:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace BinaryFormatterClass
    {
        class Program
        {
            static void Main(string[] args)
            {
                Person p = new Person();
                p.Name = "卡卡西";
                p.Age = 20;
    
                #region 序列化
                
                using (FileStream fs = new FileStream("1.csv", FileMode.Create))
                {
                    BinaryFormatter bf = new BinaryFormatter();
                    bf.Serialize(fs, p);
                }
                Console.WriteLine("序列化成功了");
                Console.ReadKey();
    
    
                #endregion
    
    
                #region 反序列化
                
    
                using (FileStream fs = new FileStream("1.csv", FileMode.Open, FileAccess.Read))
                {
                    BinaryFormatter bf = new BinaryFormatter();
                    object obj = bf.Deserialize(fs);
                    string name = ((Person)obj).Name;
                    int age = ((Person)obj).Age;
                    Console.WriteLine(name+"----"+age);
                    Console.ReadKey();
                }
    
                #endregion
    
            }
        }
        [Serializable]
        class Person
        {
            private string _name;
            private int _age;
            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }
    
            public int Age
            {
                get { return _age; }
                set { _age = value; }
            }
        }
    }
  • 相关阅读:
    图像处理基础2
    c++之morphologyEx(形态学操作)
    图像处理基础
    Mac 安装QT
    Qmake VS Cmake
    g++,qmake,cmake区别
    C++11中的匿名函数(lambda函数,lambda表达式)
    c++相关要点
    spritekit基础节点学习
    spriteKit简单学习
  • 原文地址:https://www.cnblogs.com/ink-heart/p/5900048.html
Copyright © 2011-2022 走看看