Winform(6.序列化方式)
今天写一下序列化的方式,在这之前先下载一个包(如下图)
下完包后创建三个类,分别是Student,Person,People
Student代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
namespace _3.序列化方式
{
[Serializable]
//继承序列化接口
public class Student : ISerializable
{
private string _id;//私有属性
public string ID//公开字段
{
get
{
return _id;
}
set
{
_id = value;
}
}
//
public string ID2;//2>>1
public int Age { get; set; }
public string Name { get; set; }
public Student() {
}
//特殊的构造函数,用作反序列化
//SerializationInfo:存储序列化和反序列化信息
//StreamingContext:数据流上下文
/*
在反序列化中调用
*/
protected Student( SerializationInfo info,StreamingContext context)
{
Console.WriteLine("----------反序列化-----******------");
Name = info.GetString("Name");
Age = info.GetInt32("Age");
}
/*
在序列化过程中调用,目的是把对象保存到SerializationInfo中
一般都在该方法中添加序列化的字段。