user1018711
user1018711

Reputation:

Reacting on serialize and deserialize of an instance

I have a class which i am serializing. I annotated it with [Serializable] and i am using binary serializer. Everything works fine.

But later i introduced new properties, which cannot be serialized (lets say they contain a lot of mess about GUI which does not need to be rembered). I can compute these properties based on other properties of class.

I need to do it two times, when I serialize - clean mess and enter stabile state ready for serialization. And deserialization - again compute all needed properties.

I need to react on 'events' instance is being serialized/deserialized.

However I can't find these events because I am not implementing the interface ISerializable or abstract class Aserializable but only class atribute [Serializable].

I do not know when class is being serialized because it is not the concern of this class; it is serialized as a field of another class.

Is there a way I can react on those events?

Upvotes: 4

Views: 776

Answers (3)

Vinzz
Vinzz

Reputation: 4028

Have you considered per chance the [XmlIgnoreAttribute] attribute? It will prevent a property to be serialized. No need to tamper withe the serialization workflow.

My bad, didn't realize you wanted to reload some property on deserialization. So why not serialize these? In an optional subObject, or whatever?

Upvotes: 0

Emmanuel N
Emmanuel N

Reputation: 7449

Build Custome Serialization by Implementing ISerializable. Use OnSerializingAttribute, to manipulate object before serilazation and OnDeserializingAttribute, to manipulate before deserialization.

Upvotes: 0

Dan Bryant
Dan Bryant

Reputation: 27515

You can use OnDeserializedAttribute and its related attributes (OnSerializing, OnSerialized, OnDeserializing) to create special methods that are called during the serialization/deserialization process.

Upvotes: 2

Related Questions