How to deserialize a revised object?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm saving an object to isolated storage to preserve user session
information. Over time I add new variables to the session object. This breaks
the deserialization of the object with an
System.Runtime.Serialization.SerializationException. The exception message
reads "Possible Version mismatch. Type
raptSessionData.Aralan.dbLibrary.raptSessionData has 3 members,
number of members deserialized is 2." Cool! It sees what the problem is.

How do I amend the code below to deserialize the two data members stored in
the old version of the object?

Thanks in advance for your help!

Sub restore()
Dim isf As IsolatedStorageFile, needsInit As Boolean = False
isf = IsolatedStorageFile.GetUserStoreForAssembly()
If isf.GetFileNames(RAPTSESSION_FLNM).Length > 0 Then
Dim isfs As IsolatedStorageFileStream, binFmt As BinaryFormatter,
rsd As raptSessionData
isfs = New IsolatedStorageFileStream(RAPTSESSION_FLNM,
FileMode.Open, isf)
binFmt = New BinaryFormatter
Try
rsd = DirectCast(binFmt.Deserialize(isfs), raptSessionData)
Me.setTo(rsd)
Catch ex As System.Runtime.Serialization.SerializationException
needsInit = True
Catch ex As Exception
Throw ex
Finally
isfs.Close()
End Try
Else
needsInit = True
End If
If needsInit Then
Me.initSessionVariables()
End If
isf.Close()
End Sub

Hal Heinrich
VP Technology
Aralan Solutions Inc.
 
Hi Scott,

Thanks for your reply! I used your XML suggestion - storing the data in a
fabricated dataset and using WriteXml / ReadXml. Then it's simple enough to
trap the 'column not found' error when an expected field doesn't exist
because it's been newly added.

I did look into custom serialization, but it's not straightforward to
implement.

Hal
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top