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?