2011-03-22 5 views
0

체스 게임의 상태를 저장하려고합니다. 예외의직렬화 예외

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; 
    } 

} 

유형 :

유형 'WindowsFormsApplication1.Pieces'총회에서 'ChessBoardGame, 버전 = 1.0.0.0는, PublicKeyToken = null의 문화 = 중립'으로 직렬화 표시되지 않습니다.

+1

안녕하세요 드미트리. StackOverflow에서 만족 스러우면 (체크 표시를 클릭하여) 답변을 수락하는 것이 일반적입니다. 당신은 14 가지 질문 (이 질문 포함)을 받았지만 받아 들였습니다. 계속하면 어떤 사람들은 당신을 돕기를 거절 할 수도 있습니다. 자세한 내용은 FAQ를 읽어보십시오. – MPelletier

+0

나는 어제 수락 틱을 주었고, 오늘 주겠다. –

+0

고마워. 과거의 질문을 잊지 마십시오. 그들은 또한 중요합니다. – MPelletier

답변

7

아마 WindowsFormsApplication1.Pieces를 [Serializable]으로 표시해야합니까? :)

+2

+1 스마일을 추가 한 이후로) – jgauffin

+0

이름 공간을 만들어 줄 수는 없습니다. 그리고 하나의 정적 배열 Pieces 타입을 가지고 있는데, 하나의 클래스에 위의 serialize 가능한 속성을 두는 것입니다. –

+0

결코 마음에 들지 않습니다. 나는 직렬화 가능한 속성을 몇개의 클래스 위에 두어야 만했다. 그리고 지금은 작동한다 !!!! –