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”

相关文章:

  • 2022-02-02
  • 2022-01-24
  • 2022-12-23
猜你喜欢
  • 2021-09-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-18
  • 2021-11-14
  • 2021-11-19
相关资源
相似解决方案