I've written a custom class MyClass
and marked it with the <Serializable()>
attribute. I have a set of binary files on my hard drive that I've serialized using BinaryFormatter
that came from instances of MyClass
.
I've recently changed the structure of MyClass
slightly (added some properties, deleted some properties, changed a few methods, etc).
What happens when I try to deserialize the existing objects to this changed class using the code below? I've tried it and not had an error thrown or anything - but surely it can't deserialize properly when the class has changed? Is there a way I can get some useful information out of the serialized files even though I've updated the class?
Thanks.
Here's the code I'm using to do the serialization:
Public Sub serializeObject(ByVal obj As Object, ByVal outFilename As String)
Dim fStream As FileStream
Try
fStream = New FileStream(outFilename, FileMode.Create)
Dim bfmtr As New BinaryFormatter
bfmtr.Serialize(fStream, obj)
fStream.Close()
Catch ex As Exception
MsgBox("Failed to serialize: " & ex.Message)
Throw
End Try
End Sub
And to do the deserialization I'm using:
myObj = CType(deserializeObject("C:\myobject.bin"), MyClass))
Where deserializeObject
is:
Public Function deserializeObject(ByVal srcFilename As String) As Object
If File.Exists(srcFilename) Then
Dim fStream As Stream = File.OpenRead(srcFilename)
Dim deserializer As New BinaryFormatter
Dim returnObject As Object = deserializer.Deserialize(fStream)
fStream.Close()
Return returnObject
Else
Throw New ApplicationException("File not found: " & srcFilename)
End If
End Function