bwells Posted March 16, 2003 Posted March 16, 2003 (edited) I have serveral classes derived from Form and UserControl. These classes get their data from a set of classes I wrote which are declared as being serialized. When I try and save the data for the serialized classes, I get an error saying one of my UserControl derived classes is not marked as serializable (see attached for message). Indeed, the class that the dialog is identifying is not marked as serializable, and I dont want it to be serialized. I figured out that my use of an event object in my data class is causing the problem. If I comment out the event declaration below, my application works. So my data class is trying to serialize the event object. But if I try and use [NonSerialized] in front of the event declaration, I get a compiler error saying [NonSerialized] is only valid on fields. public delegate void DataChanged( Object source, COSPData data ); private event DataChanged m_notifyDataChangedClient; So what can I do to prevent my data class from trying to serialize the object associated with the event? If I cannot mark it as [NonSerialized], then wha else can I do? thanks Bryan Edited March 16, 2003 by bwells Quote
*Gurus* divil Posted March 16, 2003 *Gurus* Posted March 16, 2003 You may have to implement ISerializable to explicitly control which members are serialized, by default everything is. And, like you said, NonSerializedAttribute can only be applied to fields. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
bwells Posted March 17, 2003 Author Posted March 17, 2003 (edited) Ok. I tried implementing ISerializable. My base class where the problem is with the event has no data, so it is simple to do. But when I run the program, I get an error dialog saying that no constructor to deserialize the object was found. The class in question is a derived class. So it is acting as if that I have to implement the ISerializable interface on all derived classes as well as the base class. Does this seem right? I only want to change the serilaizlation behavior on the base class and I want the derived classes to do the "regular" thing. Can you tell me if I implement the ISerializable interface on a base class, must I also implement this on a derived class too? I have over 20 derived classes and I dont want to implement the ISerializable interface on all of them just because of one event in the base class. Perhaps you have an idea of how I could wrap the event data member so I can have it available to all my derived classes, but yet it is not part of the serialization? The event is designed to be used to notify dependents when the data changes. That is how it got tied into the data classes. I am open to any ideas! thanks Bryan Edited March 17, 2003 by bwells Quote
*Gurus* divil Posted March 17, 2003 *Gurus* Posted March 17, 2003 What about declaring the event as protected? That way it is not public, but is still available to derived classes. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
bpayne111 Posted March 17, 2003 Posted March 17, 2003 i believe that all derived classes must be declared <Serializable> as well.. i had a similiar issue and i was forced to add this to all my classes to get it to work. it's funny how me and you are having the same troubles at about the same time... good luck Quote i'm not lazy i'm just resting before i get tired.
bwells Posted March 18, 2003 Author Posted March 18, 2003 My solution was to move the event out of the base class into a wrapper class. The wrapper class is just a pass through which the base class uses to provide the interface I wanted. By wrapping the event in another class, I was able to use the NonSerialized attribute on the wrapper data member and then everything worked fine. Some of this stuff is like poking around in the dark, trying to find your way trhough a maze. Thanks! Bryan Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.