Response to instance serialization and deserialization

I have a class that I am serializing. I annotated it using [Serializable] and I am using a binary serializer. Everything is working fine.

But later I introduced new properties that cannot be serialized (say, they contain a lot of mess in the GUI that need not be re-read). I can calculate these properties based on other class properties.

I need to do this two times when I serialize - a clean mess and enter a stable state ready for serialization. And deserialization - again calculate all the necessary properties.

I need to respond to "events", the instance is serialized / deserialized.

However, I cannot find these events because I do not implement the ISerializable interface or the abstract Aserializable class, but only the class atribute [Serializable] .

I do not know when a class is serialized because it does not apply to this class; it is serialized as a field of another class.

Is there a way to respond to these events?

+4
source share
3 answers

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

+2
source

Build Custome Serialization by implementing ISerializable. Use OnSerializingAttribute to manipulate the object before serialization and OnDeserializingAttribute to manipulate before deserialization.

0
source

Do you find the [XmlIgnoreAttribute] attribute random? This will prevent serialization of the object. There is no need to influence the serialization workflow.

My bad one, I didn’t understand that you want to reload some property during deserialization. So why not serialize them? An optional subObject or something else?

0
source

Source: https://habr.com/ru/post/1391275/


All Articles