连续序列化保存类实例计数,然后保存类实例,然后反序列化类计数及其实例。一种写入方法和一种读取方法,使用格式化程序进行序列化和反序列化。保存所有当前状态,包括生命计数和游戏状态、玩家位置和其他属性。例如看到这个:
阅读:
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(op.FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
FCnt = (int)formatter.Deserialize(stream);
for (int i = 0; i < FCnt; i++)
{
facility[i] = (Facility)formatter.Deserialize(stream);
}
stream.Close();
写作 :
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream(FN, FileMode.Create, FileAccess.Write, FileShare.None);
formatter.Serialize(stream, FCnt);
for (int i = 0; i < FCnt; i++)
{
formatter.Serialize(stream, facility[i]);
}
stream.Close();