Solved

How do you implement ISerializable for inherited classes?

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

717 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