2014-01-09 4 views
0

개체 배열과 문자열을 직렬화하려고합니다.다른 유형의 여러 객체를 직렬화하는 방법은 무엇입니까?

FileStream s; 
s = new FileStream(openfile.FileName, FileMode.Open, FileAccess.Read); 
BinaryFormatter bf = new BinaryFormatter(); 
string password = (string)bf.Deserialize(s); 
ch.chaps = (Chapter[])bf.Deserialize(s); 
s.Close(); 
int i; 

if (password == txtPassword.Text) 
{ 
    for (i = 0; i <= 1000; i++) 
    { 
     try 
     { 
      combChapSelect.Items.Add(ch.chaps[i].chapName);   
     } 
     catch 
     { 
      i = 1000; 
     } 
    } 
} 

이 코드와 비주얼 스튜디오는이 오류가 없다하지만 파일을 선택하면 OpenFileDialog를이 닫히지 않습니다 말한다 :

FileStream s; 
s = new FileStream(savefile.FileName, FileMode.Create, FileAccess.Write); 
BinaryFormatter bf = new BinaryFormatter(); 
bf.Serialize(s, ch.chaps); 
bf.Serialize(s, txtPassword.Text); 
s.Close(); 

이 역 직렬화 코드는 다음과 같습니다이 직렬화 코드 아무 일도 일어나지 않습니다. 내가 잘못한 것을했거나 다른 객체 유형을 직렬화하는 다른 방법이 있습니까?

+0

챕터가 ISerializable을 구현합니까? "직렬화"하려고하지만 코드에서 "비 직렬화"를 호출한다고 가정합니다. 그래서 당신은 serialize하거나 deserialize하려고합니까? –

답변

1

이렇게하면됩니다. 직렬화와 동일한 순서로 비 직렬화해야합니다.

직렬화에서 chaps-then-password. 당신의 비 직렬화에서, 그 비밀 번호를 누른 다음.

+0

고마워요! 그게 효과가 있었어. 나는 그 명령이 중요하다는 것을 몰랐다. – Serenical

관련 문제