Brendan said:
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
--