Extending Serialized Objects

  • Thread starter Thread starter Russ
  • Start date Start date
R

Russ

Hi,



What's the easiest way of modifying an object which has been serialized to a
DB? The previous developer created a complex object hierarchy and binary
serialized the whole thing to a single field in SQL server. I need to
modify the object hierarchy, but every time I do so, I can no longer
deserialize the incoming data because my updated object has more members
than the original object.



The object is about 15 levels deep and contains several custom collections,
arraylists and other stuff, so I'm looking to automate this if possible.
I've looked briefly at reflection (which I believe may hold the answer) but
if there's already some popular method of achieving this out there.. Surely
someone else must have had the same problem?



Thanks,

Russ
 
Hi Russ,

It seems that there is version change in your object.

The .NET Framework provides support for versioning and side-by-side
execution, and all classes will work across versions if the interfaces of
the classes remain the same. Because serialization deals with member
variables and not interfaces, be cautious when adding member variables to,
or removing them from, classes that will be serialized across versions.
This is especially true for classes that do not implement the ISerializable
interface. Any change of state of the current version, such as the addition
of member variables, changing the types of variables, or changing their
names, will mean that existing objects of the same type cannot be
successfully deserialized if they were serialized with a previous version.

If the state of an object needs to change between versions, class authors
have two choices:

Implement ISerializable. This allows you to take precise control of the
serialization and deserialization process, allowing future state to be
added and interpreted correctly during deserialization.
Mark nonessential member variables with the NonSerialized attribute. This
option should only be used when you expect minor changes between different
versions of a class. For example, when a new variable has been added to a
later version of a class, the variable can be marked as NonSerialized to
ensure the class remains compatible with previous versions.

Object Serialization in the .NET Framework
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/ht
ml/objserializ.asp

Here are the articles about Customized Serialization

Custom Serialization
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconCustomSerialization.asp

Run-time Serialization
http://msdn.microsoft.com/msdnmag/issues/02/04/net/

Run-time Serialization, Part 2
http://msdn.microsoft.com/msdnmag/issues/02/07/net/

Run-time Serialization, Part 3
http://msdn.microsoft.com/msdnmag/issues/02/09/net/

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top