[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How do you implement ISerializable for inherited classes?

Posted on 2013-01-17
2
Medium Priority
?
595 Views
Last Modified: 2013-01-18
I have a class ThisWontSerialize that inherits from ParentThisWontSerialize, both of which implement ISerializable. When I save an instance of the first class in Session, the data for the property from the parent class doesn't save.

How do I get the data from both classes to serialize?

Here are the serialization methods from the first class:

        public ThisWontSerialize(SerializationInfo info,StreamingContext context): base()
        {
            MyObject1 = (string)info.GetValue("MyObject1", typeof(string));
            MyObject2 = (string)info.GetValue("MyObject2", typeof(string));
            MyObject3 = (PeerThisWontSerialize)info.GetValue("MyObject3", typeof(PeerThisWontSerialize));
                       
            // ParentObject1 = (string)info.GetValue("ParentObject1", typeof(string));
            
        }

        // Implement this method to serialize data. The method is called  
        // on serialization. 
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            base.GetObjectData(info,context);

            // Use the AddValue method to specify serialized values.
            info.AddValue("MyObject1", MyObject1, typeof(string));
            info.AddValue("MyObject2", MyObject2, typeof(string));
            info.AddValue("MyObject3", MyObject3, typeof(PeerThisWontSerialize));
            // info.AddValue("ParentObject1", ParentObject1, typeof(string));

        }

Open in new window


Here are the serialization methods from the parent class:

        public ParentThisWontSerialize(SerializationInfo info,StreamingContext context)
        {
            ParentObject1 = (string)info.GetValue("ParentObject1", typeof(string));
        }

        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            // Use the AddValue method to specify serialized values.
            info.AddValue("ParentObject1", ParentObject1, typeof(string));
        }

Open in new window


As you can see I tried explicitly serializing the parent property but this just gives an error - you can't do it.

I'll attach the code I've been working with.
SqlSessionTest.zip
0
Comment
Question by:purplesoup
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 1500 total points
ID: 38789529
0
 

Author Comment

by:purplesoup
ID: 38792288
ok well thanks, there was quite a lot to read through and when I got to the bit that was relevant to what I was doing it just said:

"Serializing the base type's fields is easy if the base type also implements the ISerializable interface—you just call the base type's GetObjectData method."

which of course was what I was doing, so I went back and looked at my code again and discovered I wasn't passing the info and context parameters down to the base class for the overloaded constructor - so this was my code:

public ThisWontSerialize(SerializationInfo info,StreamingContext context): base()

Open in new window


and this is what it should have been

public ThisWontSerialize(SerializationInfo info,StreamingContext context): base(info,context)

Open in new window

0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question