//使用序列化保存对象状态到存储介质
//添加[Serializable]
Game game = new Game();
game.Level = 2;
game.Player = "Tom";
FileStream fs = new FileStream(@"game.bin",FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs,game);
//使用反序列化从存储介质读取对象状态
Game game = new Game();
FileStream fs = new FileStream(@"game.bin",FileMode.Open,FileAccess.Read);
BinaryFormatter bf = new BinaryFormatter();
game = (Game)bf.Deserialize(fs);
时间: 2024-10-11 23:22:22