C#的序列化和反序列化

C#的序列化和反序列化

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
// using System.Runtime.Serialization.Formatters.Soap;  // SoapFormatter类
// using System.Runtime.Serialization.Json;  // DataContractJsonSerializer类用于序列化Json使用

namespace MySerializable
{
    /// <summary>
    /// 用于序列化的实体类
    /// </summary>
    [Serializable]
    class Student
    {
        public string Name { get; set; } = "XXX";
        
        public string Sex { get; set; } = "男";
        
    	/// <summary>
    	/// 不序列化此字段
    	/// </summary>
        [NonSerialized]
        public string NotSerializable;
    }
	
    /// <summary>
    /// 用于自定义序列化的实体类
    /// </summary>
    [Serializable]
    class Person : ISerializable
    {
        public string Name { get; set; } = "XXX";

        public string Sex { get; set; } = "男";

        public string Others { get; set; } = "其他";
        public Person()
        { }

        public Person(SerializationInfo info, StreamingContext context)
        {
            Name = info.GetString("Name");
            Sex = info.GetString("Sex");
            Others = info.GetString("Others");
        }

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            info.AddValue("Name", Name);
            info.AddValue("Sex", Sex);
            info.AddValue("Others", Others);
        }
    }
    
	class Program
    {
        static void Main(string[] args)
        {
            #region 使用自动序列化
            Student stu = new Student() { NotSerializable = "当前字段不序列化"};
            // 序列化
            MemoryStream stream = new MemoryStream();
            BinaryFormatter formatter = new BinaryFormatter();
            formatter.Serialize(stream, stu);

            // 因为反序列用的是同一个对象,所以需要将Stream的位置设置为起始位置
            stream.Position = 0;
            // 反序列化
            var deStu = (Student)formatter.Deserialize(stream);
            // 输出结果
            Console.WriteLine("Name:" + deStu.Name);
            Console.WriteLine("Sex:" + deStu.Sex);
            Console.WriteLine("NotSerializable:" + deStu.NotSerializable);
            Console.WriteLine();
            #endregion

            #region 使用自定义序列化
            // 使用自定义序列化
            Person person = new Person();
            MemoryStream stream1 = new MemoryStream();
            BinaryFormatter formatter1 = new BinaryFormatter();
            formatter1.Serialize(stream1, person);

            // 因为反序列用的是同一个对象,所以需要将Stream的位置设置为起始位置
            stream1.Position = 0;
            // 反序列化
            var dePerson = (Person)formatter1.Deserialize(stream1);
            // 输出结果
            Console.WriteLine("Name:" + dePerson.Name);
            Console.WriteLine("Sex:" + dePerson.Sex);
            Console.WriteLine("Others:" + dePerson.Others);
            Console.WriteLine();
            #endregion

            Console.ReadLine();
        }
    }
}

注意:在编写序列化相关的代码时,最好编写成泛型类或生成相关动态库,以方便代码重用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值