8
votes

I have created a non-visual component in C# which is designed as a placeholder for meta-data on a form.
The component has a property which is a collection of custom objects, this object is marked as Serializable and implements the GetObjectData for serilizing and public constuctor for deserilizing.

In the resx file for the form it will generate binary data for storing the collection, however any time I make a change to the serialized class I get designer errors and need to delete the data manually out of the resx file and then recreate this data.

I have tried changing the constuctor to have a try / catch block around each property in the class

try
{
  _Name = info.GetString("Name");
}
catch (SerializationException)
{
  this._Name = string.Empty;
}

but it still crashes. The last error I got was that I had to implement IConvertible.

I would prefer to use xml serialization because I can at least see it, is this possible for use by the designer?

Is there a way to make the serialization more stable and less resistant to changes?

Edit:
More information...better description maybe
I have a class which inherits from Component, it has one property which is a collection of Rules. The RulesCollection seems to have to be marked as Serializable, otherwise it does not retain its members.

The Rules class is also a Component with the attribute DesignTimeVisible(false) to stop it showing in the component tray, this clas is not marked Serializable.

Having the collection marked as Serializable generates binary data in the resx file (not ideal) and the IDE reports that the Rules class is not Serializable.

I think this issue is getting beyond a simple question. So I will probably close it shortly.
If anyone has any links to something similar that would help a lot.

3

3 Answers

3
votes

You might want to try the alternate approach of getting everything to serialize as generated code. To do that is very easy. Just implement your non-visual class from Component. Then expose your collection as you already are but ensure each object placed into the collection is itself derived from Component. By doing that everything is code generated.

3
votes

I have since discovered where I was going wrong.

The component I was implementing a custom collection (inherited from CollectionBase), I changed this to a List and added the DesignerSerializationVisibility(DesignerSerializationVisibility.Content) attribute to the List property, this list is also read-only. This would then produce code to generate all the components properties and all the entries in the List.

The class stored in the list did not need any particuar attributes or need to be serializble.

private List<Rule> _Rules;
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public List<Rule> Rules
{
    get { return _Rules; }
}
2
votes

Could you put more code up of the class that is having the serialization issue, maybe the constructor and the property to give reference to the variables you're using.

Just a note: I've had a lot of issues with the visual designer and code generation, if I've got a property on a control then generally I put

[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]

on the property and handle the initialization myself.