Why can't the 'NonSerialized' attribute be used at the class level? How to prevent serialization of
Posted
by ck
on Stack Overflow
See other posts from Stack Overflow
or by ck
Published on 2010-03-18T03:20:55Z
Indexed on
2010/03/18
3:31 UTC
Read the original article
Hit count: 355
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.
- Ideally, I'd like .NET to just stop going down the object graph at that point and make that a 'leaf'. So why can't I just mark the handler class 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?
© Stack Overflow or respective owner