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; }
            }
        }
    }
  • 相关阅读:
    活动安排问题
    完美字符串
    Codeforces Round #696 (Div. 2) 解题报告
    Codeforces 1459D
    Codeforces 25D
    POJ 1847
    LightOJ 1074
    POJ 3159
    POJ 1511
    POJ 1502
  • 原文地址:https://www.cnblogs.com/ink-heart/p/5900048.html
Copyright © 2011-2022 走看看