using System;
using System.Dynamic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace ConsoleApp1
{
[Serializable]
class A
{
public string Test = "123";
}
class Program
{
static void Main(string[] args)
{
var a = new A();
//序列化
using(var steam=File.Open(typeof(A).Name+".bin",FileMode.Create))
{
var bf = new BinaryFormatter();
bf.Serialize(steam, a);
}
A after = null;
//反序列化
using(var steam = File.Open(typeof(A).Name + ".bin", FileMode.Open))
{
var bf = new BinaryFormatter();
after= (A)bf.Deserialize(steam);
}
Console.WriteLine(after.Test);
Console.Read();
}
}
}