Changing 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
 
You can customize the construtor that was built for deserialization to
handle the cases where your new fields don't exist. For example, if you add
an Integer called X to the object, you can wrap the code in the constructor
to retrieve the value for X in a Try...Catch and put some default value in
if you cannot retrieve the value:

Public Sub New(ByVal info As SerializationInfo, ByVal context As
StreamingContext)
....
Try
Me.X= CType(info.GetValue("X", GetType(Integer)), Integer)
Catch ex As SerializationException
' X is not in the object being deserialized - assign a default value
Me.X = 0
End Try
....
End Sub



Brian Davis
http://www.knowdotnet.com
 
Back
Top