1
votes

Is it possible to use the BinaryFormatter Serialize/Deserialize methods to serialize and deserialize a class that contains UI objects? I know it's likely not recommended and only data should really be serialized, but is there a reason why it would not be supported technically?

Say, for example, I had a class such as:

public class MyClass
{
    private List<UC1> _ucList1 = new List<UC1>();
    private List<UC2> _ucList2 = new List<UC2>();

    public MyClass() {}
}

In the above theoretical class, UC1 and UC2 are of type UserControl - so a list of user controls. Is there a reason why that could not be deserialized? I did some testing and it appears to serialize into a byte array fine, but the deserialization routine throws an exception "End of Stream encountered before parsing was completed."

1

1 Answers

0
votes

No, you should use the standard Component Serialization. The binary formatter will ignore for example extended properties, design only properties and will not invoke the BeginInit/EndInit of components that implement ISupportInitialize. I don't know what it would do on Delegates (Events), probably it will fail to serialize them.

Finally if I remember well none of the classes in WinForms is decorated with the Serializable attribute