簡體   English   中英

如何序列化不同類型的多個對象?

[英]How to serialize multiple objects of different types?

我正在嘗試序列化對象數組和字符串。 這是序列化代碼:

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();

這是反序列化代碼:

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

這是代碼,Visual Studio表示沒有錯誤,但是當我選擇一個文件並且沒有任何反應時,openfile對話框沒有關閉。 我做錯了什么,還是有另一種方法來序列化不同的對象類型?

您正在這樣做。 您必須按照序列化的順序反序列化。

在您的序列化過程中,它首先破解了密碼。 在反序列化過程中,其密碼隨后會破裂。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM