------------------------------------13:38 2015/1/22------------------------------------------------
序列化与反序列化
//要将序列化对象的类,标记为可以被序列化的
Person p=new Person();
p.Name="张三";
using(FileStream fsWrite=new FileStream(@"c:
ew.txt",FileMode.OpenOrCreate,FileAccess.Write))
{
BinaryFormatter bf=new BinaryFormatter();
bf.Serialize(fsWrite,p);
}
Console.WriteLine("序列化成功!S");
//反序列化
Person p;
using(FileStream fsWrite=new FileStream(@"c:
ew.txt",FileMode.OpenOrCreate,FileAccess.Read))
{
BinaryFormatter bf=new BinaryFormatter();
bf.Deserialize(fsRead);
p=(Person)bf.Deserialize(fsRead);
}
Console.WriteLine(p.Name);
Console.ReadKey();
[Serializable]
public class Person
{
public string Name
{
get;
set;
}
}