Serializing inherited objects

  • Thread starter Mark Saccomandi
  • Start date
M

Mark Saccomandi

Hi,

I'm extending a serializable class I made. The new class adds some features
that cannot be serialized automatically, this means that I have to implement
the ISerializable interface for my new class. What happens is that I do
serialize what I have written within the GetObjectData method, but now I do
not get to automatically serialize the data from my parent class.
Is there a way to serialize automatically the properties from my parent
class and then to serialize manually only what I have added in the derived
class?

Here's a sample of the code:

[Serializable]

public class OperDataSocketIn : Operator, ISerializable

{

[NonSerialized]

private DataSocket dataSocketIn;

...

private OperDataSocketIn( SerializationInfo info, StreamingContext
context)

{

dataSocketIn =new DataSocket();

dataSocketIn.Url= info.GetString("Url");

}



void ISerializable.GetObjectData(SerializationInfo info,
StreamingContext context)

{

info.AddValue("Url", dataSocketIn.Url);

}

...

}

Using this code after deserializing I get an object that has empty values
except for dataSocketIn.Url.

Thanks, Mark Saccomandi
 
?

=?ISO-8859-2?Q?Marcin_Grz=EAbski?=

Hi Mark,

If you want serialize/deserialize inherited object (base class)
then you should provide to call its serialization/deserialization
code.

e.g.


// constructor form deserialization
private OperDataSocketIn(SerializationInfo info, StreamingContext context)
: base( info, context)
{
// your code
}

// serialization method
public override void GetObjectData(SerializationInfo info,
StreamingContext context)
{
base.GetObjectData(info, context);

// your code
}

Regards

Marcin
 
P

Patrik Löwendahl [C# MVP]

Hi Mark,

It's my understanding that implementing the ISerializable interface
somewhere in the inheritance chain, effectivly shuts the automatic
serialization process of.

When one think about this, it is only natural. Which type is it that will
get serialized? The subclass type of course, and that's where the selection
of serialization method ought to be. If the subclass implements
ISerializable, the serializer chooses to use that interface for your object.
 
N

Nicholas Paldino [.NET/C# MVP]

Mark,

Basically, you are going to have to perform the serialization yourself.
However, there is an easy way to do this.

What you can do is in your implementation of the serialization
constructor, you would make a call to the static GetSerializableMembers
method on the FormatterServices class. This will give you an array of
MemberInfo instances representing the serializable members of your class.
At that point, you could cycle through them, and get the values using
reflection, or you could pass the array to the static GetObjectData method
on the FormatterServices class.

The only thing you have to do is make sure that you remove the
MemberInfo instances that you want to serialize yourself.

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top