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 :)

At first glance your code seems fine. (Except for the lack of error handling.)
What errors are you getting?

What is "IBGSDraw"?

The cast should be of the same type as shapes.
Which, from how you described you structures, I presume is List<IDraw>.

Is IBGSDraw of type List<IDraw>?

instead of IBGSDraw you need to just put the name of the class of the object shapes:
I'm using the code bellow and it's working fine:

The Object is : MyProject of class clsPrject

clsProject MyProject= new clsProject;

.....Code to input your object data....

Serialization:

            FileStream filestream = new
                       FileStream(@"C:\OutputFile.dzv", FileMode.Create);
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(filestream, MyProject);
            filestream.Close();

Deserialization:

        DialogResult result = openProjectFileDialog.ShowDialog();
        openProjectFileDialog.Filter = "DS Files (*.dzv)|*.dzv";

        if (result == DialogResult.OK) // 
        {
            try
            {

                using (Stream input = File.OpenRead(openProjectFileDialog.FileName))
                {
                    BinaryFormatter bf = new BinaryFormatter();
                    MyProject = (clsProject) bf.Deserialize(input);
                }



            }
            catch (IOException)
            {
                //Error 

            }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.