views:

212

answers:

2

Hi,

I am trying to find out the best way to handle serialisation in my program.

I want to serialize private fields (so XML serializer is no option) I want to serialize generic types like Bindinglist (SoapSerializer is no option) I also want to provide some level of support for being able to deserialize object from previous versions of my program.

So all I think I am left with is the BinarySerializer, and implementing the ISerializable interface to cope with forward compatibility.

Is this correct?

Note: I must say I found this project. However the project seemed dead and I couldn't get it working (file missing error) http://sourceforge.net/project/showfiles.php?group_id=196509

+1  A: 

With BinaryFormatter, you can use [OptionalField], but this is still very brittle.

How about protobuf-net? Does all that, and doesn't have the versioning issues associated with BinaryFormatter. Since it is based on Google's "protocol buffers" format, it is designed to be version tolerant and extensible (disclosure: I'm the author, so I may be biased).

Alternatively, DataContractSerializer supports field serialization; just set [DataMember] against your fields instead of the properties (requires .NET 3.0).

Marc Gravell
A: 

Yes, you'd need to implement ISerializable.

The key points are the following

  • Implement GetObjectData(SerializationInfo info, StreamingContext context). Here you need to add key pairs into info object.
  • Implement serialization constructor MyClass(SerializationInfo info, StreamingContext context). Here you'd need to get the values from the info object.
Martin Clarke