简体   繁体   English

序列化异常

[英]Serialization Exception

i am trying to save a state of a chessGameplay. 我试图保存国际象棋游戏的状态。

private void saveToolStripMenuItem_Click(object sender, EventArgs e)// menu strip control
{
    saveFileDialog1.InitialDirectory = @"c:\";

    DialogResult result= saveFileDialog1.ShowDialog();
    if (result == DialogResult.OK)
    {
        saveToFile(saveFileDialog1.FileName);
    }

}

private void loadToolStripMenuItem_Click(object sender, EventArgs e)
{
    openFileDialog1.InitialDirectory = @"c:\";

    DialogResult result= openFileDialog1.ShowDialog();
    if (result == DialogResult.OK)
    {
        openFile(openFileDialog1.FileName);
    }
}
GameSave game2 = new GameSave();
public void saveToFile(string s)
{
    game2.setLoadedPieces(codeFile.PieceState());// will pass the current pieces state. that is an array of all the chess pieces objects..which determine where each piece is on the board
    FileStream f = new FileStream(s, FileMode.Create);
    BinaryFormatter b = new BinaryFormatter();

    b.Serialize(f, game2);// throws here an exception.Type 'WindowsFormsApplication1.Pieces' in Assembly 'ChessBoardGame, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.
    f.Close();
}

public void openFile(string s)
{
    FileStream f = new FileStream(s, FileMode.Open);// will open the file and the stream
    BinaryFormatter b = new BinaryFormatter();
    game2 = (GameSave)b.Deserialize(f);// will load the stream
    f.Close();
    codeFile.setPieces(game2.getLoadedPieces());// sets the board to the loaded pieces.
    PrintPieces(game2.getLoadedPieces());//prints the existing loaded pieces.
}


[Serializable] 
class GameSave
{
    Pieces[,] pieces;


    public void setLoadedPieces(Pieces[,] serializedSavedPieces) // set the pieces array  to be saved
    {       
        this.pieces = serializedSavedPieces; 
    }
    public Pieces[,] getLoadedPieces() // returns the pieces array
    {
        return pieces;
    }

}

Type of exception: 例外类型:

Type 'WindowsFormsApplication1.Pieces' in Assembly 'ChessBoardGame, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable. 程序集“ ChessBoardGame,版本= 1.0.0.0,区域性=中性,PublicKeyToken = null”中的类型“ WindowsFormsApplication1.Pieces”未标记为可序列化。

Perhaps you should mark WindowsFormsApplication1.Pieces as [Serializable] ? 也许您应该将WindowsFormsApplication1.Pieces标记为[Serializable] :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM