Reputation: 11090
We have a fairly complex object model, that we have the need to save a current state at a particualr time, and in the event of a particular issue to revert the state back to what it was. Pretty much an undo function.
I initially considered a deep copy, but finally settled on simply serializing the model to binary and then deserializing if we need to. This is the first time that i've done something like this, and initially had issues with eventhandlers not serializing, which I solved with the [field: NonSerialized] attribute.
The issue now however is that when the model is deserialized, then events do not work.
What would be the best solution to deal with this? I've come across a couple of solutions, I could either use the [OnDeserialized()] or implement the IDeserializationCallback interface, and then in the subsequent methods re-subscribe to each of the events.
This does seem like quite a lot of work though, and as i've no experience with this, I just wanted to check that this is the correct way before I plough on ahead.
Thanks.
Upvotes: 1
Views: 367
Reputation: 46008
You cannot serialize event handlers that are attached to your events in object model. You will need to resubscribe after you deserialize your model.
I would go for the IDeserializationCallback interface, as you need to notify other classes to resubscribe to the instances that you've just deserialized.
Upvotes: 2