Solved

How do you implement ISerializable for inherited classes?

Posted on 2013-01-17
2
584 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
2 Comments
 
LVL 29

Accepted Solution

by:
anarki_jimbel earned 500 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now