using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Runtime.Serialization.Formatters.Binary;
namespace _07序列化和反序列化
{
[Serializable]
class MyClass
{
string name;
public string Name
{
get { return name; }
set { name = value; }
}
int age;
public int Age
{
get { return age; }
set { age = value; }
}
char gender;
public char Gender
{
get { return gender; }
set { gender = value; }
}
}
//创建序列化
MyClass m = new MyClass();
m.Name = "张三";
m.Age = 18;
m.Gender = '男';
using (FileStream file = new FileStream("t.data", FileMode.Create, FileAccess.Write))
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(file, m);
}
Console.WriteLine("OK");
Console.ReadKey();
//反序列化
using (FileStream file = new FileStream("t.data", FileMode.Open, FileAccess.Read))
{
BinaryFormatter bf = new BinaryFormatter();
MyClass c = bf.Deserialize(file) as MyClass;
}
Console.ReadKey();