Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serializing events

I have class A, which exposes an event. an object of class B subscribed to the event. Both instances actually also hold a regular reference to eachother. I'd like to serialize A, and have both objects be transferred over the wire, to be reconstructed at the other end. This works fine, except that the event subscription is not retained.

I think I have to implement my own serialization constructor in order to 'be notified' when my objects gets deserialized, so it can resubscribe to the event.

However, it looks like that means I have to completely implement seialization myself by implementing ISerializable. I now rely on BinaryFormatter, and [Serializable] attributes, and actually like that very much.

Does anybody know a way for me to get notified on deserialization, while still being able to use the built in serialization system to automatically serialize & deserialize my somewhat complex object graph for me?

Thanks, Lucas

like image 988
Lucas Avatar asked May 15 '09 16:05

Lucas


1 Answers

The standard (non-XML) serialization mechanism uses special attributes to mark methods to be used as callbacks during (de)serialization . OnDeserializedAttribute is what you're looking for. Similar solution is implementing IDeserializationCallback interface.

like image 132
felixg Avatar answered Sep 19 '22 06:09

felixg