A08_序列化与反序列化



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace A01_SerializeAndDeserialize
{
    [Serializable]//可序列化且不可被继承
    class Person
    {
        private string _Name;

        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }

        private int _Age;

        public int Age
        {
            get { return _Age; }
            set { _Age = value; }
        }

        public Person(string name, int age)
        {
            _Name = name;
            _Age = age;
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

//序列化:将类的对象(字段以及数值),以文本的形式保存为文件。
//反序列化:创建相同的对象,然后将文本中的数值,重新赋值给对象的字段。

namespace A01_SerializeAndDeserialize
{
    class Demo1
    {
        private string strFilePath = @"G:\TestSerialize.txt";
        //序列化
        public void Test1()
        {
            Person p = new Person("王俊凯",18);
            if (File.Exists(strFilePath))
            {
                using (FileStream fs = new FileStream(strFilePath,FileMode.OpenOrCreate))
                {
                    BinaryFormatter bf = new BinaryFormatter();
                    bf.Serialize(fs, p);
                }
            }
        }

        //反序列化
        public void Test2()
        {
            Person p = null;
            if (File.Exists(strFilePath))
            {
                using (FileStream fs = new FileStream(strFilePath, FileMode.OpenOrCreate))
                {
                    BinaryFormatter bf = new BinaryFormatter();
                    p = bf.Deserialize(fs) as Person;
                }
            }
            Console.WriteLine(p.Name);
            Console.WriteLine(p.Age);
            
        }

        static void Main(string[] args)
        {
            Demo1 obj = new Demo1();
            //obj.Test1();
            obj.Test2();
        }
    }
}




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace A01_SerializeAndDeserialize
{
    [Serializable]//可序列化且不可被赋值
    class Person
    {
        private string _Name;

        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }

        private int _Age;

        public int Age
        {
            get { return _Age; }
            set { _Age = value; }
        }

        public Person(string name, int age)
        {
            _Name = name;
            _Age = age;
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

//序列化:将类的对象(字段以及数值),以文本的形式保存为文件。
//反序列化:创建相同的对象,然后将文本中的数值,重新赋值给对象的字段。

namespace A01_SerializeAndDeserialize
{
    class Demo1
    {
        private string strFilePath = @"G:\TestSerialize.txt";
        //序列化
        public void Test1()
        {
            Person p = new Person("王俊凯",18);
            if (File.Exists(strFilePath))
            {
                using (FileStream fs = new FileStream(strFilePath,FileMode.OpenOrCreate))
                {
                    BinaryFormatter bf = new BinaryFormatter();
                    bf.Serialize(fs, p);
                }
            }
        }

        //反序列化
        public void Test2()
        {
            Person p = null;
            if (File.Exists(strFilePath))
            {
                using (FileStream fs = new FileStream(strFilePath, FileMode.OpenOrCreate))
                {
                    BinaryFormatter bf = new BinaryFormatter();
                    p = bf.Deserialize(fs) as Person;
                }
            }
            Console.WriteLine(p.Name);
            Console.WriteLine(p.Age);
            
        }

        static void Main(string[] args)
        {
            Demo1 obj = new Demo1();
            //obj.Test1();
            obj.Test2();
        }
    }
}

test




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值