473,387 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Serialization in Inherited Classes ?

I have a class inherited from Control, which I want to serialize, since
Control is not Serializable, I have had to implement ISerializable. This
works but I now want to inherit this base class into a number of other
classes, I was assuming that I could just mark them as [Serializable] and use
automatic serialization but this does not work (The new class cannot
deserialize, because it does not have the correct constructor ie one with
arguments (SerializationInfo info, StreamingContext context)). Am I being
stupid or do I need to use ISerializable on all descendent classes.
Nov 17 '05 #1
2 5054
Tony,

Unfortunately, once you choose to implement ISerializable in a base
class, every class that derives from that class needs to implement the
ISerializable interface as well as implement the serialization constructor
in order to support serialization.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"tony lock" <to******@discussions.microsoft.com> wrote in message
news:2C**********************************@microsof t.com...
I have a class inherited from Control, which I want to serialize, since
Control is not Serializable, I have had to implement ISerializable. This
works but I now want to inherit this base class into a number of other
classes, I was assuming that I could just mark them as [Serializable] and
use
automatic serialization but this does not work (The new class cannot
deserialize, because it does not have the correct constructor ie one with
arguments (SerializationInfo info, StreamingContext context)). Am I being
stupid or do I need to use ISerializable on all descendent classes.

Nov 17 '05 #2
at.
Hi Nicholas,

I saw your recent reply on microsoft.public.dotnet.languages.csharp
about custom serialization. I knew that "when you derive a new class
from one that implements ISerializable, the derived class must implement
both the constructor as well as the GetObjectData method if it has
variables that need to be serialized." (MSDN extract). But what's about
the opposite situation?

When a class B implements ISerializable and inherits from class A (which
doesn't implement ISerializable), how should I handle the
serialization/deserialization of the fields of the base class A?
Actually, before implementing ISerialible for class B, each class was in
charge of instantiating its own variable. Thus the constructor of B was
calling the constructor of A with the rights arguments. With B
implementing ISerializable, it is not possible anymore (it seems).

Any solution? Should B serialize all A fields in the GetObjectData
method and deserialize them in its special constructor?

Many thanks in advance for your advice!
Christian
Here is what I tried to do... But that doesn't work: A fields are not
(de)serialized
class A
{
public A(int i)
{ ... }
}

class B : A, ISerializable
{
public B(int i, int j) : base(i)
{ ... }

private B(SerializationInfo info, StreamingContext context)
{
j = info.GetInt32("j")
}
[SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter=true)]
public virtual void GetObjectData(SerializationInfo info,
StreamingContext context)
{
info.AddValue("j", j);
}
}
Nicholas Paldino [.NET/C# MVP] a écrit :
Tony,

Unfortunately, once you choose to implement ISerializable in a base
class, every class that derives from that class needs to implement the
ISerializable interface as well as implement the serialization constructor
in order to support serialization.

Hope this helps.

Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ot | last post by:
I have two different solutions that use xml serialization to create xml from a given class. The class is defined identically in both solutions. I copied the code for both the class and the code...
0
by: ron | last post by:
This is as clean as i can get it. Thanks Ron This is the base class that serializes and desrializes the class objects it also provides abstract methodes that are called from its inherited...
3
by: Aaron Clamage | last post by:
Hi, I'm not sure that if this is the right forum, but any help would be greatly appreciated. I am porting some java serialization code to c# and I can't figure out the correct way to do it. ...
0
by: Stephen | last post by:
I am trying to return from a WebMethod a class called "TreeResponse" which is inherited from the base class "Response". It appears the properties in the base class "Response" are not being...
1
by: David C | last post by:
I am new to Serialization, so please bear with me. I have a class inherited from a non-serializable class. My goal is to serialize an instance of it to XML and I am only interested in the...
1
by: Tamir Khason | last post by:
I have a base class MyBaseFoo with some properties, methods etc. I have some derived classes, implements small part of MyBaseFoo and a lot of their own. In order to ensure proper deserialization...
1
by: Glenn Wilson | last post by:
In the current project that I am planning i am thinking of serializing the objects to save as binary files. The question I have is with inheriting classes. If I have 1 base class and 2 classes...
10
by: SStory | last post by:
My app is near completed for the basic feature of version 1.0. I have an extensive object model and I now want to persist my objects using serialization. I have chosen binaryformatter to...
4
by: mijalko | last post by:
Hi, I have inherited my class from System.Drawing.Printing.PrintDocument and I wish to serialize this object using XmlSerializer. And I get exception "There was an error reflecting type ...". If I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.