Solved

How do you implement ISerializable for inherited classes?

Posted on 2013-01-17
2
592 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 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

733 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