Event serialization

I have a class A that provides an event. an object of class B subscribed to an event. Both instances actually also contain a regular link to eachother. I would like to serialize A and pass both objects through the wiring to be reconstructed at the other end. This works great except that event subscriptions are not saved.

I think I need to implement my own serialization constructor in order to β€œbe notified” when my objects are deserialized, so it can re-subscribe to the event.

However, it looks like this means that I have to fully implement seialization myself, implementing ISerializable. Now I rely on the attributes of BinaryFormatter and [Serializable], and actually so much.

Does anyone know how to get a deserialization notification while still being able to use the built-in serialization system to automatically serialize and deserialize my somewhat complex graph of objects for me?

Thanks Lucas

+6
c # events serialization
source share
1 answer

The standard (non-XML) serialization engine uses special attributes to indicate methods that will be used as callbacks during (de) serialization. OnDeserializedAttribute is what you are looking for. A similar solution implements the IDeserializationCallback interface.

+8
source share

All Articles