序列化与反序列化实例
结果,废话不说上代码,希望对初学者有所帮助。如有问题请联系博主,QQ:593658550
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections;
namespace Ex_5_1_1_序列化与反序列化
{
class Program
{
static void Main(string[] args)
{
//Test.EmployeeTest();
EmployeeCollege<Employee> employeeCollege = EmployeeCollege<Employee>.InitEmployeeCollege();
EmployeeCollege<Employee>.Print(employeeCollege);
EmployeeCollege<Employee>.Serialize(employeeCollege);
employeeCollege = EmployeeCollege<Employee>.DeSerialize();
Console.WriteLine("反序列化输出:");
EmployeeCollege<Employee>.Print(employeeCollege);
Console.ReadLine();
}
private static void EmployeeTest()
{
Console.WriteLine("请输入员工信息");
Console.Write("请输入Id(数字)");
int id = Convert.ToInt32(Console.ReadLine());
Console.Write("请输入名称:");
string name = Convert.ToString(Console.ReadLine());
Employee employee = new Employee() { Id = id, Name = name };
var serialPath = "employee.txt";
Console.WriteLine("序列化");
employee.PrintSelf();
SerializeHelper.EnSerialize(employee, serialPath);
Console.WriteLine("反序列化");
employee = SerializeHelper.DeSerialize(serialPath);
employee.PrintSelf();
}
}
public class Test
{
public static void EmployeeTest()
{
Employee employee = Employee.InitEmployee();
var serialPath = "employee.txt";
Console.WriteLine("序列化");
employee.PrintSelf();
SerializeHelper.EnSerialize(employee, serialPath);
Console.WriteLine("反序列化");
employee = SerializeHelper.DeSerialize(serialPath);
employee.PrintSelf();
}
}
public class SerializeHelper
{
public static void EnSerialize(Employee employee,string serialPath)
{
FileStream fs = new FileStream(serialPath, FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, employee);
fs.Close();
fs.Dispose();
}
public static Employee DeSerialize(string serialPath)
{
Employee employee = new Employee();
FileStream fs = new FileStream(serialPath, FileMode.Open, FileAccess.ReadWrite, FileShare.Read);
BinaryFormatter bf = new BinaryFormatter();
employee = (Employee)bf.Deserialize(fs);
//Console.WriteLine("员工姓名:{0}", employee.Name);
fs.Close();
fs.Dispose();
return employee;
}
}
/// <summary>
/// 测试序列化对象
/// </summary>
[Serializable]
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public void PrintSelf()
{
Console.WriteLine($"ID:{Id}\r\nName:{Name}");
}
public static Employee InitEmployee()
{
Console.WriteLine("请输入员工信息");
Console.Write("请输入Id(数字)");
int id = Convert.ToInt32(Console.ReadLine());
Console.Write("请输入名称:");
string name = Convert.ToString(Console.ReadLine());
Employee employee = new Employee() { Id = id, Name = name };
return employee;
}
}
[Serializable]
public class EmployeeCollege <TEmployee> : CollectionBase
{
public TEmployee this[int index]
{
get { return (TEmployee)List[index]; }
set { List[index] = value; }
}
public EmployeeCollege()
{
}
public static EmployeeCollege<Employee> InitEmployeeCollege()
{
Console.WriteLine("初始化员工集合对象");
EmployeeCollege<Employee> employeeCollege = new EmployeeCollege<Employee>();
bool isRun = true;
while (isRun)
{
Employee employee = Employee.InitEmployee();
employeeCollege.Add(employee);
Console.WriteLine("是否继续添加用户,是选择1.否选择0");
isRun = Console.ReadLine()=="1"?true:false;
}
return employeeCollege;
}
public static void Serialize(EmployeeCollege<Employee> employeeCollege)
{
string employeeCollegePath = "EmployeeCollege.data";
FileStream fs = new FileStream(employeeCollegePath, FileMode.OpenOrCreate);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs,employeeCollege);
fs.Close();
fs.Dispose();
}
public static EmployeeCollege<Employee> DeSerialize()
{
EmployeeCollege<Employee> employeeCollege = new EmployeeCollege<Employee>();
string employeeCollegePath = "EmployeeCollege.data";
FileStream fs = new FileStream(employeeCollegePath,FileMode.Open);
BinaryFormatter binaryFormatter = new BinaryFormatter();
employeeCollege= (EmployeeCollege<Employee>)binaryFormatter.Deserialize(fs);
fs.Close();
fs.Dispose();
return employeeCollege;
}
public static void Print(EmployeeCollege<Employee> employeeCollege)
{
Console.WriteLine("打印员工集合信息:");
foreach (Employee employee in employeeCollege)
{
Console.WriteLine($"ID={employee.Id}\tName={employee.Name}");
}
Console.WriteLine("打印结束");
}
public void Add(TEmployee employee)
{
List.Add(employee);
}
public void Insert(int index, TEmployee employee)
{
List.Insert(index,employee);
}
public void Remove(TEmployee employee)
{
List.Remove(employee);
}
public bool Contains(TEmployee employee)
{
return List.Contains(employee);
}
}
}