Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Design time serialization in C#

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.

like image 830
benPearce Avatar asked Oct 01 '08 01:10

benPearce


People also ask

What are the types of serialization?

Basic and custom serialization Binary and XML serialization can be performed in two ways, basic and custom.

What is ISerializable C#?

The ISerializable interface implies a constructor with the signature constructor (SerializationInfo information, StreamingContext context) . At deserialization time, the current constructor is called only after the data in the SerializationInfo has been deserialized by the formatter.

What is Deserialized?

Deserialization is the process of reconstructing a data structure or object from a series of bytes or a string in order to instantiate the object for consumption. This is the reverse process of serialization, i.e., converting a data structure or object into a series of bytes for storage or transmission across devices.

What is Serialisation and Deserialisation?

Serialization is a mechanism of converting the state of an object into a byte stream. Deserialization is the reverse process where the byte stream is used to recreate the actual Java object in memory. This mechanism is used to persist the object. The byte stream created is platform independent.


3 Answers

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.

like image 188
Phil Wright Avatar answered Nov 15 '22 07:11

Phil Wright


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; }
}
like image 43
benPearce Avatar answered Nov 15 '22 07:11

benPearce


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.

like image 38
Jiminy Avatar answered Nov 15 '22 07:11

Jiminy