Groups | Blog | Home
all groups > dotnet sdk > november 2003 >

dotnet sdk : Serialization inheritance


Ilya Evseev
11/28/2003 1:28:57 AM
Hi, folks!
There is some class in external library
that is declared with [Serializable()] attribute.
I need to create class inherited from them
that should be serializable too.
My class uses fields calculated at deserialization stage,
so it's declared not as following:
[Serializable()]
class MyClass : BaseClass { ... }

but as following (look all !!! and ???'s carefully:-):

class MyClass : BaseClass, ISerializable
{
protected int SimpleField; // serializable
protected int CalculatedField; // non-serializable

public MyClass(
SerializationInfo info,
StreamingContext ctx)
// : base(info, ctx) // error!!!
{
// my stuff
info.Add("SimpleField", SimpleField);
}
public virtual void GetObjectData(
SerializationInfo info,
StreamingContext ctx)
{
// base.GetObjectData(info, ctx); // error!!!
// my stuff
SimpleField = info.GetInteger("SimpleField");
// how to conform next line
// with calling of inherited deserialization routine???
CalculatedField = SomethingVerySpecial(SimpleField);
}
}

Well, I don't know how to call inherited constructor
and GetObjectData from my own constructor/GetObjectData
appropriately, because this functionality in base class
is generated implicitly and cannot be called by name.

How to solve this problem?

WBR, Ilya

Bennie Haelen
12/3/2003 5:13:08 PM
Hi Ilya,

Consider moving the "base" call up as in:

[quoted text, click to view]
...
}

Regards,

Bennie Haelen

[quoted text, click to view]

Jacek Helka
1/13/2004 9:35:07 AM
Hi,
There is a FormatterServices class that can help you.

MemberInfo[] members =
FormatterServices.GetSerializableMembers(typeof(BaseClass), context);
object[] data = FormatterServices.GetObjectData(obj, members);
for(int i = 0; i < members.Length; i++)
info.AddValue(members[i].Name, data[i]);

Deserialization is similar:

MemberInfo[] members =
FormatterServices.GetSerializableMembers(typeof(BaseClass), context);
object[] data = new object[members.Length];
for(int i = 0; i < members.Length; i++)
data[i] = info.GetValue(members[i].Name, data[i]);
FormatterServices.PopulateObjectMembers(obj, members, data);



[quoted text, click to view]

AddThis Social Bookmark Button