473,698 Members | 2,330 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Serializing inherited objects

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 OperDataSocketI n : Operator, ISerializable

{

[NonSerialized]

private DataSocket dataSocketIn;

...

private OperDataSocketI n( SerializationIn fo info, StreamingContex t
context)

{

dataSocketIn =new DataSocket();

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

}

void ISerializable.G etObjectData(Se rializationInfo info,
StreamingContex t context)

{

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

}

...

}

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

Thanks, Mark Saccomandi
Nov 16 '05 #1
5 2650
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 OperDataSocketI n(Serialization Info info, StreamingContex t context)
: base( info, context)
{
// your code
}

// serialization method
public override void GetObjectData(S erializationInf o info,
StreamingContex t context)
{
base.GetObjectD ata(info, context);

// your code
}

Regards

Marcin
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 OperDataSocketI n : Operator, ISerializable

{

[NonSerialized]

private DataSocket dataSocketIn;

...

private OperDataSocketI n( SerializationIn fo info, StreamingContex t
context)

{

dataSocketIn =new DataSocket();

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

}

void ISerializable.G etObjectData(Se rializationInfo info,
StreamingContex t context)

{

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

}

...

}

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

Thanks, Mark Saccomandi

Nov 16 '05 #2
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.

--
Patrik Löwendahl [C# MVP]
cshrp.net - 'Elegant code by witty programmers'
cornerstonel.se - 'IT Training for proffessionals'

"Mark Saccomandi" <ma************ *@a.team.nu> wrote in message
news:OY******** ******@TK2MSFTN GP14.phx.gbl...
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 OperDataSocketI n : Operator, ISerializable

{

[NonSerialized]

private DataSocket dataSocketIn;

...

private OperDataSocketI n( SerializationIn fo info, StreamingContex t
context)

{

dataSocketIn =new DataSocket();

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

}

void ISerializable.G etObjectData(Se rializationInfo info,
StreamingContex t context)

{

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

}

...

}

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

Thanks, Mark Saccomandi

Nov 16 '05 #3
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 GetSerializable Members
method on the FormatterServic es 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 FormatterServic es 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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"Mark Saccomandi" <ma************ *@a.team.nu> wrote in message
news:OY******** ******@TK2MSFTN GP14.phx.gbl...
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 OperDataSocketI n : Operator, ISerializable

{

[NonSerialized]

private DataSocket dataSocketIn;

...

private OperDataSocketI n( SerializationIn fo info, StreamingContex t
context)

{

dataSocketIn =new DataSocket();

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

}

void ISerializable.G etObjectData(Se rializationInfo info,
StreamingContex t context)

{

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

}

...

}

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

Thanks, Mark Saccomandi

Nov 16 '05 #4
Thanks. Your answer was quite helpfull.

Mark
Nov 16 '05 #5
Interesting solution.
I'll try and see how it works.
Thanks,
Mark
Nov 16 '05 #6

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

Similar topics

6
2495
by: Andreas | last post by:
Hello list, what about uniqueness of inherited primary keys ? eg you have : create table objects ( id int4, date_created timestamp(0), primary key (id)
5
6901
by: Jeff Bunting | last post by:
I'm trying to serialize a class I have and have been getting: An unhandled exception of type 'System.InvalidOperationException' occurred in system.xml.dll There was an error reflecting type MyClass. I noticed in the help for the SerializableAttribute Class the following: "Indicates that a class can be serialized. This class cannot be inherited." The class I'm trying to serialize is not inherited itself, but does inherit
1
2084
by: Ivo Bronsveld | last post by:
All, I have quite a challenging task ahead of me. I need to write an object model (for code access) based on a schema, which cannot be made into a dataset because of it's complexity. So I created a couple of objects and serializing it into XML based upon the schema works perfectly. The XML / Schema looks something like this:
6
2523
by: iucon | last post by:
Hello. I'm trying to serialize an inherited Class by a Webservice. Here ist a Code-Snippet of the Classes I try to serialize: // Base-Class public class Test { private string _Argument1; public string Argument1
2
3517
by: Tobias Zimmergren | last post by:
Hi, just wondering what serializing really is, and howto use it? Thanks. Tobias __________________________________________________________________ Tobias ICQ#: 55986339 Current ICQ status: + More ways to contact me __________________________________________________________________
4
3616
by: Dave Veeneman | last post by:
When does serializing objects make more sense than persisting them to a database? I'm new to object serialization, and I'm trying to get a feel for when to use it. Here is an example: I'm writing an accounting application. I have a chart of accounts in the form of a containment hierarchy. A GeneralLedger contains a number of Accounts, and each of these Accounts can contain a Aubledger, which contains its own Accounts, and so on. The...
0
2768
by: j.askey | last post by:
Hi, I have a class that is inherited from Panel. Because Panel is not serializable, I have implemented the ISerializable interface on this class and I can sucessfully save and load classes from disk by serializing and deserializing with a binary formatter. Now, I have another class that happens to contain an array of the above classes... when I attempt to serialize the array I get an error stating that Panel is not serializable. Here...
47
3102
by: Max | last post by:
Due to the behaviour of a particular COM object, I need to ensure that a request for a particular ASP page is finalized before another request for the page is processed. Does IIS have a way to ensure that subsequent requests will be queued until the current request is completed? If not, can IIS be configured to use seperate processes to satisfy requests for a nominated ASP page? Thanks in advance.
1
1246
by: Karthik1979 | last post by:
I have a custom class inherited from List<T> collection. Along with the base class functionality, I have included my additional properties. When serializing, only the base class items are serialized not my additional properties. I have designed a business object collection like this and I have to pass it across the service layer. But in another end I’m getting partial data not all my information is getting de-serialized. Here is a sample...
0
8604
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9160
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...
1
8897
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
8862
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
7729
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4370
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
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2331
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.