[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.sdk

Binary Formatter, serializing and deserializing

David Levine

10/8/2002 3:49:00 PM

Given two classes:
class A
{
public string sz1;
}

class B : A
{
new public string sz; // this hides inherited member A.sz1
}

if I serialize/deserialize this using the formatter without implementing the
ISerializable interface it works fine, or if I implement ISerializable in
class B and then serialize/deserialize the data it also works fine, but if
generate the data using the layout above and then modify class B to...

class B : A, ISerializable
{
new public string sz;

/// some new fields added here

public B(Serializationinfo si,StreamingContext ctx)
{
/// deserialization code not shown uses FormatterServices
}
public void GetObjectData(Serializationinfo si,StreamingContext ctx)
{
/// serialization code not shown uses FormatterServices.
}
}

void SomeMethod(Stream strm)
{
// read in data that was previously saved before the ISerializable
interface was added to class B
BinaryFormatter fmt = new BinaryFormatter();
fmt.Deserialize(strm);
}

the call to Deserialize throws an exception before the special constructor
ever gets invoked and before I can override it. The exception is "cannot add
the same member twice to the SerializationInfo object.". The offending
member is the hidden field A.sz

I need to be able to read in data that was generated using the binary
formatter by classes that did not implement ISerializable, but which have
been modified at a later date to implement that interface. This layout is
the only instance I've run into that caused problems.

Any ideas on how to work around this?

Dave