C#二进制序列化和反序列化
public class WRSerializable { public static void SerializeToFile<T>(T _description, string _filePath) { using (Stream fStream = File.Create(_filePath)) { BinaryFormatter binFormat = new BinaryFormatter();//创建二进制序列化器 binFormat.Serialize(fStream, _description); } } public static void DeserializeFromFile<T>(ref T _description, string _filePath) { using (FileStream fileStream = File.OpenRead(_filePath)) { BinaryFormatter binaryFormatter = new BinaryFormatter(); _description = (T)binaryFormatter.Deserialize(fileStream); } } // public static void SerializeToFile<T>(T _description, string _filePath) // { // FileStream fs = new FileStream(_filePath, FileMode.OpenOrCreate); // BinaryFormatter bf = new BinaryFormatter(); // bf.Serialize(fs, _description); // fs.Close(); // } // public static void DeserializeFromFile<T>(ref T _description, string _filePath) // { // FileStream fs = new FileStream(_filePath, FileMode.Open); // BinaryFormatter bf = new BinaryFormatter(); // _description = (T)bf.Deserialize(fs); // fs.Close(); // } }
filePath示例“mData.bin”