二进制泛型序列化.
public static class BinarySerializationHelper { public static void ToBinaryFile<T>(this T tempSerializeList, string path) // 二进制序列化 { //路径 var fs = new FileStream(path, FileMode.Create); try { BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(fs, tempSerializeList); fs.Close(); } catch (Exception ex) { } finally { fs.Close(); } } public static T FromBinaryFile<T>(string path) // 二进制反序列化 { T tempDeserializeList; FileInfo binaryFile = new FileInfo(path); if (!binaryFile.Exists) { return default(T); } FileStream fs = new FileStream(path, FileMode.Open); try { BinaryFormatter bf = new BinaryFormatter(); tempDeserializeList = (T)bf.Deserialize(fs); return tempDeserializeList; } catch (Exception ex) { File.Delete(path); return default(T); } finally { fs.Close(); } } }