serialization

  • Thread starter Thread starter Perecli Manole
  • Start date Start date
P

Perecli Manole

I have a class that has been serialized and saved to disk. I am trying to
deserialize it back into the same class which now has an extra private
member. It will not deserialize because its signature has changed so I
added:

Protected Sub New(ByVal info As SerializationInfo, ByVal context As
StreamingContext)
End Sub

to my new class so that I can specify custom deserialization however it does
not get called by the deserialization formatter if I don't specify that the
class implements iSerializable. When I do implement iSerializable in my new
class then the class signature no longer matches the old class so an error
gets raised. Seems like I am stuck. Maybe if I had implemented iSerializable
from the beginning in the old class this might have worked but what should I
do now?

Perry
 
Perecli,
One of the following 3 part MSDN Magazine column on Serialization covers
this problem and how to correct it!

http://msdn.microsoft.com/msdnmag/issues/02/04/net/
http://msdn.microsoft.com/msdnmag/issues/02/07/net/
http://msdn.microsoft.com/msdnmag/issues/02/09/net/

I hope you read all three, as they are invaluable for implementing
Serialization in .NET!

You are correct in needing to implement ISerializable and give the special
constructor, in addition to implementing ISerializable you need to 'Override
the type when deserializing', which is covered in the 3rd article. It
involves creating a new binder class that changes the type for version 1.0
objects to version 2.0 object.

Hope this helps
Jay
 
Yes, I have read all three articles on your previous recomendation. This is
what got me so far but it is still not working. This is how my
deserialization binder class looks:

Private Class VersionDeserializationBinder
Inherits SerializationBinder

Public Overrides Function BindToType(ByVal assemblyName As
String, ByVal typeName As String) As Type
Return Type.GetType(String.Format("{0}, {1}", typeName,
Reflection.Assembly.GetExecutingAssembly.FullName))
End Function
End Class

Still have the same problem mentioned in my initial post.

Perry
 
Perecli,
Not sure what to offer, as your code does not appear at all like the sample
in Figure 3 of article 3.

Are all the types you are deserializing in the same assembly as the
VersionDeserializationBinder class?

What you may want to do is put a break point in BindToType to see what is
coming in and what is going out, just to be certain that you have
implemented it correctly.

Hint, I suspect the routine is called for types in the System namespace,
such as System.String.

Hope this helps
Jay
 
Not sure what to offer, as your code does not appear at all like the
sample
in Figure 3 of article 3.

The sample shows how to deserialize a class with one name into a class with
a differet name. This will not work for my application because all the code
in my app references the class by name.

For example. I serialized a class with the name "MyClass" and saved it to
disk. After this I modified "MyClass" by adding a private member. Now I want
to deserialize the class on disk back into the same class "MyClass", not
another class as is shown in the example.
Are all the types you are deserializing in the same assembly as the
VersionDeserializationBinder class?
Yes

What you may want to do is put a break point in BindToType to see what is
coming in and what is going out, just to be certain that you have
implemented it correctly.

I have done this and all is OK. The version of the old class is changed to
that of the current assembly.
Hint, I suspect the routine is called for types in the System namespace,
such as System.String.

The problem is that when I add "Implements ISerializable" to my new class,
with no other changes as compared to the old class that was serialized. The
deserializer complains that it can't deserialize it to the new class. Keep
in mind that my old class did not implement ISerializable.

Perry
 
Perecli,
Not sure what to offer, I gave you all the tricks I know on the subject.

Asking in either microsoft.public.dotnet.framework or
microsoft.public.dotnet.framework.clr might find someone with more knowledge
on Serialization.

Good luck,
Jay
 
Back
Top