Serialization and Event Publishers
Posted on 2005-03-01
I have an application consisting of Form A and Class B. Class B stores a set of values that I want to save to file, and is therefore Serializable. However, I also added an event to it so that when one of the values changed, a subscriber (in this case Form A) was notified, and could refresh itself.
What actually happened was that when I attempted to write Class B to a filestream, it failed with a 'NonSerializable object' error - the object in question was the form.
Is this because I've added the form to Class B as a subscriber, and therefore .NET assumes it must also serialize the form? Assuming it is, what is the 'best practice' for dealing with this situation, bearing in mind that I can't mark an event as NonSerializable? Encapsulate Class B within another class that publishes the event? (I was hoping to avoid this, as it takes the responsibility of its own objects away from Class B, and means every change to variables within Class B will have to go through this wrapper).