C#学习总结-序列化和反序列化
class A
{
public string Test = "123";
}
class Program
{
static void Main(string[] args)
{
var a = new A();
//序列化
using (var stream = File.Open(typeof(A).Name + ".yfs", FileMode.Create))
{
var bf = new BinaryFormatter();
bf.Serialize(stream, a);
}
//反序列化
A after = null;
using (var stream = File.Open(typeof(A).Name + ".yfs", FileMode.Open))
{
var bf = new BinaryFormatter();
after=(A)bf.Deserialize(stream);
}
Console.WriteLine(after.Test);
Console.ReadLine();
}
}