序列化与反序列化:
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Runtime.Serialization.Formatters.Binary; using System.Text; using System.Threading.Tasks; namespace BinaryFormatterClass { class Program { static void Main(string[] args) { Person p = new Person(); p.Name = "卡卡西"; p.Age = 20; #region 序列化 using (FileStream fs = new FileStream("1.csv", FileMode.Create)) { BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(fs, p); } Console.WriteLine("序列化成功了"); Console.ReadKey(); #endregion #region 反序列化 using (FileStream fs = new FileStream("1.csv", FileMode.Open, FileAccess.Read)) { BinaryFormatter bf = new BinaryFormatter(); object obj = bf.Deserialize(fs); string name = ((Person)obj).Name; int age = ((Person)obj).Age; Console.WriteLine(name+"----"+age); Console.ReadKey(); } #endregion } } [Serializable] class Person { private string _name; private int _age; public string Name { get { return _name; } set { _name = value; } } public int Age { get { return _age; } set { _age = value; } } } }