473,687 Members | 3,538 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 (SerializationI nfo info, StreamingContex t context)). Am I being
stupid or do I need to use ISerializable on all descendent classes.
Nov 17 '05 #1
2 5076
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.co m

"tony lock" <to******@discu ssions.microsof t.com> wrote in message
news:2C******** *************** ***********@mic rosoft.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 (SerializationI nfo info, StreamingContex t 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.publi c.dotnet.langua ges.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(Serialization Info info, StreamingContex t context)
{
j = info.GetInt32(" j")
}
[SecurityPermiss ionAttribute(Se curityAction.De mand,Serializat ionFormatter=tr ue)]
public virtual void GetObjectData(S erializationInf o info,
StreamingContex t 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
1404
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 that does the serialization from the first solution to the second. The first solution works just fine. The second solution gets a weird error.
0
423
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 classes. namespace FDS.DataCollectionServer.DataProcessing
3
3172
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. It seems that either I can use default serialization or implement ISerializable. Is there any way to do both (e.g. extend the default serialization). In other words, I want to be able to implement my custom serialization code but call the...
0
1078
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 serialized into the WSDL but the properties from the inherited class "TreeResponse" are being serialized. I have tried the following with no success: 1. Adding attribute to both classes 2. Adding on TreeResponse
1
1288
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 properties of the inherited class, not those of the base class. Is there a way to select which properties are to be serialized from my class so that the XML serializer does not bother with the non-serializable base class?
1
2771
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 on versioning MyBaseFoo implements ISerializable and override constructor So following public void GetObjectData(SerializationInfo info, StreamingContext context) {
1
1891
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 that inherit from it. How would I configure them for serialization. Do I just put the Serialized setting on the 2 classes or do I also have to put it on the base class. ie.
10
1481
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 serialize, and custom serialization, which I understand will allow me the flexibility of not breaking old things when I add members to classes in the future and send to existing customers. 1.) is there anything else to consider with the custom...
4
11409
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 look at innerException it says: "Cannot serialize member 'System.ComponentModel.Component.Site' of type 'System.ComponentModel.ISite'. OK it is problem to serialize all data so I'll implement my Serialization. I implemented ISerializable...
0
9070
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8947
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8781
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8784
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5806
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4321
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4543
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2214
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1948
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.