Hi,
I have several classes for different types of shapes (line, rectangle..etc) which all implements interface IDraw.
And then I have a List<IDraw> called shapes which I want to Serialize/Deserialize.
I think I got the Serialization right:
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.InitialDirectory = @"C:\";
saveFileDialog1.Filter = "Object Files (*.dat)|*.dat";
DialogResult result = saveFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
using (Stream output = File.Create(saveFileDialog1.FileName))
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(output, shapes);
}
}
}
But I have some problems with the deserialization process.
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = @"C:\";
openFileDialog1.Filter = "Oject Files (*.dat)|*.dat";
DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
using (Stream input = File.OpenRead(openFileDialog1.FileName))
{
BinaryFormatter bf = new BinaryFormatter();
shapes = (IBGSDraw)bf.Deserialize(input);
}
}
}
There is some problemt with line 12 here. I can't type an interface type inside the parenthesis. I've just read chapter FILE I/O AND OBJECT SERIALIZATION in 'Pro C# 2010 and the .NET 4 Platform' without finding answer to this. Hope someone can help me a little bit :)