序列化
//要序列化的类型 [Serializable] public class myclass { public string Name {get;set;} } //序列化 var my = new myclass(); my.Name="1234"; my.SerializeSave("D:\\data.txt"); //反序列化 var my2 = "D:\\data.txt".DeSerialize<myclass>(); public static class Ex{ static BinaryFormatter form = new BinaryFormatter(); /// <summary> /// 序列化保存到文件 /// </summary> /// <param name="serializable"></param> /// <param name="path"></param> public static void SerializeSave(this object serializable, string path) { using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write)) { form.Serialize(fs, serializable); } } /// <summary> /// 反序列化 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="path"></param> /// <returns></returns> public static T DeSerialize<T>(this string path) where T : class, ISerializable { using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read)) { return form.Deserialize(fs) as T; } } }