views:

2352

answers:

5

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.

+2  A: 

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.

Phil Wright
It seems that this method also produces binary serialized data, plus every object in the collection is represented on the component tray
benPearce
Prevent it appearing in the tray using...[ToolboxItem(false)]...on the class. I use this approach all the time and do not get binary data. So you must be doing something that is forcing that to happen. Can you post a snippet of code?
Phil Wright
+1  A: 

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.

Jiminy
where abouts do you handle the initilization? Is there are required interface or is it in the constructor?
benPearce
In the constructor or in the OnLoad event on the form
Jiminy
Your suggestion worked out for the guys here at work. Thanks!
Brett Veenstra
A: 

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; }
}
benPearce
A: 

what about the net CF 2.0? the atributte "DesignerSerializationVisibility" is not there...

alejandro varela
Can't help you in CF 2.0. Try starting a new question.
benPearce
A: 

Answer http://stackoverflow.com/questions/155852/design-time-serialization-in-c/199307#199307 did the trick for me. Thank you, this problem was driving me mad !

GabSoftware