Solved

How do you implement ISerializable for inherited classes?

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

810 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