I have a data object that is deep-cloned using a binary serialization. This data object supports property changed events, for example, PriceChanged.
Let’s say I attached a handler to PriceChanged. When the code attempts to serialize PriceChanged, it throws an exception that the handler isn’t marked as serializable.
My alternatives:
- I can’t easily remove all handlers from the event before serialization
- I don’t want to mark the handler as serializable because I’d have to recursively mark all the handlers dependencies as well.
- I don’t want to mark PriceChanged as NonSerialized – there are tens of events like this that could potentially have handlers. EDIT: Another reason why I can’t do this is because the data classes (and hence the events) are generated and I don’t have direct control over the generation code. Ideally, the generation code would just mark all events as NonSerialized.
- Ideally, I’d like .NET to just stop going down the object graph at that point and make that a ‘leaf’. So why doesn’t .NET allow an entire class to be marked as NonSerialized?
—
I finally worked around this problem by making the handler implement ISerializable and doing nothing in the serialize constructor/ GetDataObject method. But, the handler still is serialized, just with all its dependencies set to null – so I had to account for that as well.
Is there a better way to prevent serialization of an entire class? That is, one that doesn’t require accounting for the null dependencies?
While I tend to disagree with the approach (I would simply mark the events as NonSerialized, regardless of how many there are) you could probably do this using serialization surrogates.
The idea is that you create an object that implements ISerializationSurrogate and basically does what you’re already doing – nothing in the GetObjectData and SetObjectData methods. The difference is that you would be customizing the serialization of the delegate, not the class containing it.
Something like:
Then you register this with the formatter using the procedures outlined in this MSDN column. Then whenever the formatter encounters a delegate, it uses the surrogate instead of serializating the delegate directly.