0

我一直在关注二进制序列化指南(这里是这个:http: //www.codeproject.com/Articles/1789/Object-Serialization-using-C),我想我终于几乎让它工作了。当我保存文件时创建,但是当我尝试加载时,什么都没有加载。我觉得我已经接近完成这项工作了。任何意见,将不胜感激。这是代码:

保存/加载类

[Serializable()]
public class SaveLoad : ISerializable
{
    public int GameDay = Date.GameDay;
    public List<Adventurer> Adventurers = FormMain.AdventurerManager.AdventurerList;

    public SaveLoad()
    {
        GameDay = 0;
        Adventurers = null;
    }

    public SaveLoad(SerializationInfo info, StreamingContext ctxt)
    {
        GameDay = (int)info.GetValue("Date", typeof(int));
        Adventurers = (List<Adventurer>)info.GetValue("Adventurers", typeof(List<Adventurer>));
    }

    public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
    {
        info.AddValue("Date", GameDay);
        info.AddValue("Adventurers", Adventurers);
    }
}

保存/加载方法:

    void btnSaveGame_Click(object sender, EventArgs e)
    {
        SaveLoad save = new SaveLoad();

        Stream stream = File.Open("SaveGame.osl", FileMode.Create);
        BinaryFormatter bformatter = new BinaryFormatter();

        bformatter.Serialize(stream, save);
        stream.Close();
    }

    void btnLoadGame_Click(object sender, EventArgs e)
    {
        SaveLoad load = new SaveLoad();

        Stream stream = File.Open("SaveGame.osl", FileMode.Open);
        BinaryFormatter bformatter = new BinaryFormatter();

        load = (SaveLoad)bformatter.Deserialize(stream);
        stream.Close();

        Date.CalculateDate();

        this.Visible = false;
        ((FormMain)(this.ParentForm)).ControlMainScreen.Visible = true;
    }
4

2 回答 2

1

我认为您可能只是有初始化时间问题。

尝试将 GameDay 和 Adventurers 的初始化移动到构造函数,或者在构造函数中去掉它们的空值。完成以下操作后,代码几乎可以正常工作:

public SaveLoad()
{
    //GameDay = null;
    //Adventurers = null;
}

请注意,我还必须确保 Adventurer 类具有 Serialization 属性。

这是对我有用的带有序列化的代码(我必须创建自己的 Adventurer 类,并且我用字符串替换了日期,因为我无法弄清楚你在用它做什么或它来自哪里。我也用一些虚拟数据填充冒险者列表,并注释掉与我也没有信息的表单内容有关的任何内容。

[Serializable()]
public class SaveLoad : ISerializable
{
    public string GameDay = null;
    public List<Adventurer> Adventurers = null; 
        //FormMain.AdventurerManager.AdventurerList;

    public SaveLoad()
    {
        GameDay = "Date";
        Adventurers = new List<Adventurer>() { new Adventurer { Name = "a1", Type = "t1" }, new Adventurer { Name = "a1", Type = "t1" } }; ;
    }

    public SaveLoad(SerializationInfo info, StreamingContext ctxt)
    {
        GameDay = (string)info.GetValue("Date", typeof(string));
        Adventurers = (List<Adventurer>)info.GetValue("Adventurers", typeof(List<Adventurer>));
    }

    public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
    {
        info.AddValue("Date", GameDay);
        info.AddValue("Adventurers", Adventurers);
    }
}
[Serializable()]
public class Adventurer
{
    public string Name { get; set; }
    public string Type { get; set; }
}

private void btnLoadGame_Click(object sender, EventArgs e)
    {
        SaveLoad sl = new SaveLoad();

        Stream stream = File.Open("SaveGame.osl", FileMode.Open);
        BinaryFormatter bformatter = new BinaryFormatter();

        sl = (SaveLoad)bformatter.Deserialize(stream);
        stream.Close();

        MessageBox.Show(sl.Adventurers.Count.ToString());
        //Date.CalculateDate();

        //this.Visible = false;
        //((Form1)(this.ParentForm)).ControlMainScreen.Visible = true;

    }

    private void btnSaveGame_Click(object sender, EventArgs e)
    {
        SaveLoad sl = new SaveLoad();

        Stream stream = File.Open("SaveGame.osl", FileMode.Create);
        BinaryFormatter bformatter = new BinaryFormatter();

        bformatter.Serialize(stream, sl);
        stream.Close();

    }
于 2013-05-10T00:21:45.850 回答
0

编辑

@JasonHaley 是对的,您有时间/初始化问题。

在加载按钮单击事件期间,您定义了一个名为“加载”的新 SaveLoad

这将创建对 FormMain.AdventurerManager.AdventurerList 的引用

但是在反序列化过程中,这个引用被序列化到磁盘的对象(另一个冒险者列表)破坏,现在是一个与 FormMain.AdventurerManager.AdventurerList 中定义的不同的冒险者列表

您需要专门加载到该列表中...

void btnLoadGame_Click(object sender, EventArgs e)
{
    Stream stream = File.Open("SaveGame.osl", FileMode.Open);
    BinaryFormatter bformatter = new BinaryFormatter();

    SaveLoad load = (SaveLoad)bformatter.Deserialize(stream);
    // ***********************************
    FormMain.AdventurerManager.AdventurerList = SaveLoad.Adventurers
    // ***********************************

    stream.Close();

    Date.CalculateDate();

    this.Visible = false;
    ((FormMain)(this.ParentForm)).ControlMainScreen.Visible = true;
}
于 2013-05-10T00:01:24.463 回答