473,405 Members | 2,262 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,405 software developers and data experts.

[Serializable] Changes

I'm creating a new app that will have a number of project settings. Rather
than save all these to the registry, I'd like to create a serializable class
to define all of the settings, and save them to a project file.
I'm concerned that if I ever change the class, then the prior saved files
will be rendered useless. Is this true?
What if I just added fields, properties and methods to the class?

Thanks in advance for the help,
Dennis

Nov 17 '05 #1
6 1440
To answer this question... let me quickly go over how serialization and
deserialization works.

When you are dumping your serializable class to a file, the system uses
reflection to know the names and types of the different properties that are
to be written.

When reading from the file, the system will create an instance of the class
with the default constructor and iterate through the file, reading each
property and attempts to find a matching property in the class it created, if
it finds it, it sets the value, if not it moves onto the next one.

Because of this, the serialized output is not directly require a given
structure within the class it will be applied to, so if you add a new
property to your class and read in an old file, when complete that property
will simply have the default value as specified by the class and it’s
constructor. The upside of this too, is that if you remove a property and
load a doc that has this property, the differences will be ignored.

Brendan

"db_from_mn" wrote:
I'm creating a new app that will have a number of project settings. Rather
than save all these to the registry, I'd like to create a serializable class
to define all of the settings, and save them to a project file.
I'm concerned that if I ever change the class, then the prior saved files
will be rendered useless. Is this true?
What if I just added fields, properties and methods to the class?

Thanks in advance for the help,
Dennis

Nov 17 '05 #2
Hello Brendan,
Thanks a lot. This is great news!
This is the second time in recent days that I've run across "reflection". I
need to study up on that.
Thanks again,
Dennis

"Brendan Grant" wrote:
To answer this question... let me quickly go over how serialization and
deserialization works.

When you are dumping your serializable class to a file, the system uses
reflection to know the names and types of the different properties that are
to be written.

When reading from the file, the system will create an instance of the class
with the default constructor and iterate through the file, reading each
property and attempts to find a matching property in the class it created, if
it finds it, it sets the value, if not it moves onto the next one.

Because of this, the serialized output is not directly require a given
structure within the class it will be applied to, so if you add a new
property to your class and read in an old file, when complete that property
will simply have the default value as specified by the class and it’s
constructor. The upside of this too, is that if you remove a property and
load a doc that has this property, the differences will be ignored.

Brendan

"db_from_mn" wrote:
I'm creating a new app that will have a number of project settings. Rather
than save all these to the registry, I'd like to create a serializable class
to define all of the settings, and save them to a project file.
I'm concerned that if I ever change the class, then the prior saved files
will be rendered useless. Is this true?
What if I just added fields, properties and methods to the class?

Thanks in advance for the help,
Dennis

Nov 17 '05 #3
Brendan Grant wrote:
To answer this question... let me quickly go over how serialization
and deserialization works.

When you are dumping your serializable class to a file, the system
uses reflection to know the names and types of the different
properties that are to be written.

When reading from the file, the system will create an instance of the
class with the default constructor and iterate through the file,
reading each property and attempts to find a matching property in the
class it created, if it finds it, it sets the value, if not it moves
onto the next one.
no, each private member variable, not each property.
Also, if a private member variable is found in the serialized data (in
the info block) and not in the object it has to fill, you'll get an
exception.

This is for example shown when you serialize a sortedlist with strings
using the binary formatter in .NET 1.1 SP1 and deserialize it in .NET
1.1. .NET 1.1 SP1 added a private member variable to the string
comparer, which causes when deserializing the data on .NET 1.1 without
SP1. (I believe it's with the string comparer class, could be another
comparer class, I don't have an example ready)

The other way around of course works: if a member variable isn't
present in the data, it won't be filled.
Because of this, the serialized output is not directly require a
given structure within the class it will be applied to, so if you add
a new property to your class and read in an old file, when complete
that property will simply have the default value as specified by the
class and it’s constructor. The upside of this too, is that if you
remove a property and load a doc that has this property, the
differences will be ignored.


No, that last remark is definitely not true.

To circumvent deserialization issues, I wrote a utility class which
has methods like InfoGetString, InfoGetValue etc. which accept the info
block, name and type (if applicable) and contain a try/catch block and
return a default value if the field isn't there, for example when the
class structure changed in a new version.

This of course requires ISerializable to be implemented, but it also
makes sure every old version of the data can be read back and converted
to a new internal version without exceptions.

FB

--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Nov 17 '05 #4
Hello Frans,
Thanks for the clarifications.
Dennis

"Frans Bouma [C# MVP]" wrote:
Brendan Grant wrote:
To answer this question... let me quickly go over how serialization
and deserialization works.

When you are dumping your serializable class to a file, the system
uses reflection to know the names and types of the different
properties that are to be written.

When reading from the file, the system will create an instance of the
class with the default constructor and iterate through the file,
reading each property and attempts to find a matching property in the
class it created, if it finds it, it sets the value, if not it moves
onto the next one.


no, each private member variable, not each property.
Also, if a private member variable is found in the serialized data (in
the info block) and not in the object it has to fill, you'll get an
exception.

This is for example shown when you serialize a sortedlist with strings
using the binary formatter in .NET 1.1 SP1 and deserialize it in .NET
1.1. .NET 1.1 SP1 added a private member variable to the string
comparer, which causes when deserializing the data on .NET 1.1 without
SP1. (I believe it's with the string comparer class, could be another
comparer class, I don't have an example ready)

The other way around of course works: if a member variable isn't
present in the data, it won't be filled.
Because of this, the serialized output is not directly require a
given structure within the class it will be applied to, so if you add
a new property to your class and read in an old file, when complete
that property will simply have the default value as specified by the
class and it’s constructor. The upside of this too, is that if you
remove a property and load a doc that has this property, the
differences will be ignored.


No, that last remark is definitely not true.

To circumvent deserialization issues, I wrote a utility class which
has methods like InfoGetString, InfoGetValue etc. which accept the info
block, name and type (if applicable) and contain a try/catch block and
return a default value if the field isn't there, for example when the
class structure changed in a new version.

This of course requires ISerializable to be implemented, but it also
makes sure every old version of the data can be read back and converted
to a new internal version without exceptions.

FB

--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------

Nov 17 '05 #5
Frans Bouma [C# MVP] wrote:
Brendan Grant wrote:

To answer this question... let me quickly go over how serialization
and deserialization works.

When you are dumping your serializable class to a file, the system
uses reflection to know the names and types of the different
properties that are to be written.

When reading from the file, the system will create an instance of the
class with the default constructor and iterate through the file,
reading each property and attempts to find a matching property in the
class it created, if it finds it, it sets the value, if not it moves
onto the next one.

no, each private member variable, not each property.
Also, if a private member variable is found in the serialized data (in
the info block) and not in the object it has to fill, you'll get an
exception.

This is for example shown when you serialize a sortedlist with strings
using the binary formatter in .NET 1.1 SP1 and deserialize it in .NET
1.1. .NET 1.1 SP1 added a private member variable to the string
comparer, which causes when deserializing the data on .NET 1.1 without
SP1. (I believe it's with the string comparer class, could be another
comparer class, I don't have an example ready)

The other way around of course works: if a member variable isn't
present in the data, it won't be filled.

Because of this, the serialized output is not directly require a
given structure within the class it will be applied to, so if you add
a new property to your class and read in an old file, when complete
that property will simply have the default value as specified by the
class and it’s constructor. The upside of this too, is that if you
remove a property and load a doc that has this property, the
differences will be ignored.

No, that last remark is definitely not true.

To circumvent deserialization issues, I wrote a utility class which
has methods like InfoGetString, InfoGetValue etc. which accept the info
block, name and type (if applicable) and contain a try/catch block and
return a default value if the field isn't there, for example when the
class structure changed in a new version.

This of course requires ISerializable to be implemented, but it also
makes sure every old version of the data can be read back and converted
to a new internal version without exceptions.

FB

Along those lines, I have wondered how you can deserialize into an
object...which very often includes a collection of nodes...how do you
them delete one or more items from the collection and then serialize it
again???

-Mark
Nov 17 '05 #6
Mark Rance wrote:
Frans Bouma [C# MVP] wrote:
Brendan Grant wrote:

To answer this question... let me quickly go over how serialization
and deserialization works.

When you are dumping your serializable class to a file, the system
uses reflection to know the names and types of the different
properties that are to be written.

When reading from the file, the system will create an instance of the
class with the default constructor and iterate through the file,
reading each property and attempts to find a matching property in the
class it created, if it finds it, it sets the value, if not it moves
onto the next one.


no, each private member variable, not each property. Also, if
a private member variable is found in the serialized data (in
the info block) and not in the object it has to fill, you'll get an
exception.

This is for example shown when you serialize a sortedlist with
strings
using the binary formatter in .NET 1.1 SP1 and deserialize it in .NET
1.1. .NET 1.1 SP1 added a private member variable to the string
comparer, which causes when deserializing the data on .NET 1.1 without
SP1. (I believe it's with the string comparer class, could be another
comparer class, I don't have an example ready)

The other way around of course works: if a member variable isn't
present in the data, it won't be filled.

Because of this, the serialized output is not directly require a
given structure within the class it will be applied to, so if you add
a new property to your class and read in an old file, when complete
that property will simply have the default value as specified by the
class and it’s constructor. The upside of this too, is that if you
remove a property and load a doc that has this property, the
differences will be ignored.


No, that last remark is definitely not true.
To circumvent deserialization issues, I wrote a utility class which
has methods like InfoGetString, InfoGetValue etc. which accept the info
block, name and type (if applicable) and contain a try/catch block and
return a default value if the field isn't there, for example when the
class structure changed in a new version.

This of course requires ISerializable to be implemented, but it also
makes sure every old version of the data can be read back and converted
to a new internal version without exceptions.

FB

Along those lines, I have wondered how you can deserialize into an
object...which very often includes a collection of nodes...how do you
them delete one or more items from the collection and then serialize it
again???

-Mark


How come every time I post this question there are no responses at
all??? Is this just a very stupid question or does no one know how to
do this?
Is my question, as written unclear to people or is it something else
altogether...?

-Mark
Nov 17 '05 #7

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

Similar topics

0
by: xmail123 | last post by:
Hi, Hi, As was pointed out whatever you return from a WebMethod needs to be serializable to SOAP. An ArrayList is not serializable. I will be needing to return other data types from web...
8
by: xmail123 | last post by:
Hi, As was pointed out whatever you return from a WebMethod needs to be serializable to SOAP. An ArrayList is not serializable. I will be needing to return other data types from web methods. ...
5
by: Ben Terry | last post by:
I get the following compilation error: "The type or namespace name 'Serializable' could not be found (are you missing a using directive or an assembly reference?)" Can anyone tell me why the...
2
by: John | last post by:
Hi What is a Serializable class compared to a normal class? Thanks Regards
9
by: Developer | last post by:
Hi, How can one tell wsdl.exe/VS.NET web service proxy generatioon to to put on imported classes? For example, i fin your web service you use a class: class Data { }
1
by: cpnet | last post by:
I have a DataTable defined in a strongly-typed DataSet: public class MyDataSet: DataSet... { ... public class MyDataTable: DataTable... { ...}
3
by: Techno_Dex | last post by:
I'm wanting to create a Wrapper (or Extender depending on how you look at it) for a Serializable object that I then want to send over a webservice. Basically I want to create a Serializable Object,...
8
by: Techno_Dex | last post by:
Has anyone come up with a slick way to make Custom Serializable Objects to behave like DataSets when using WebServices? What I'm looking for is some way to force the WSDL generated code to create...
5
by: cctv.star | last post by:
I need to maintain certain data structure, shared by all instances of a class, declared as . This data structure must change whenever a new object is created or an existing object is destroyed. So...
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...
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...
0
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...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.