// TestASettingsString and TestBSettingsString are byte[]
// TestASettings and TestBSettings are two objects to be saved
In SettingsSaving, TestASettings and TestBSettings are serialized into two seperate byte arrays, and then saved (in some other function)
My question is how to recover TestASettings and TestBSettings from TestASettingsString and TestASettingsString seperately in loadsavedsettings?
That is when I do
formatter.Deserialize(stream);
How to seperate two totally different objects TestASettings and TestBSettings from formatter.Deserialize(stream)?
Thanks
private void SettingsSaving(object sender, CancelEventArgs e)
{
try
{
var stream = new MemoryStream();
var formatter = new BinaryFormatter();
formatter.Serialize(stream, TestASettings);
// TestASettingsString and TestBSettingsString are byte[]
TestASettingsString = stream.ToArray();
stream.Flush();
formatter.Serialize(stream, TestBSettings);
TestBSettingsString = stream.ToArray();
stream.Close();
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
}
private void LoadSavedSettings()
{
Reload();
// how to get TestASettings and TestBSettings from TestASettingsString and
// TestASettingsString seperately?
}
I suspect part of the problem is here:
For
MemoryStream,Flush()is a no-op. Do you actually mean:which will reset the stream, giving
TestBSettingsStringas a separate chunk of data? CurrentlyTestBSettingsStringactually contains bothTestASettingsandTestBSettings.With that done, it should just be a case of reversing things:
Note also: I suggest not using
BinaryFormatterfor persisting any thing offline (disk, database, etc).