J
Jeff Stewart
I'm just discovered Version-Tolerant Serialization and am synthesizing
it into an existing serialization scheme. What I read from the docs
indicates that in the example below the .Deserialize() statement
should throw a SerializationException indicating that field2 is
expected from the data stream but Ver02.field2 is not marked with
OptionalFieldAttribute. Yet I get no such exception when I munge the
serialization of Ver01 a bit. Why?
--
Jeff S.
///////////////
[Serializable]
public class Ver01 : ISerializable {
public string field1;
public override GetObjectData(SerializationInfo info,
StreamingContext context) {
info.AssemblyName =
typeof(Ver02).Assembly.FullName.Replace("1.0.0.0", "0.0.0.9");
info.FullTypeName = typeof(Ver02).FullName;
info.AddValue("field1", this.field1);
}
}
[Serializable]
public class Ver02 {
public string field1;
public string field2;
}
public static class ExampleClass {
public static void TryDeserialize() {
MemoryStream memStream = new MemoryStream();
// Create an old instance and serialize it to a memory stream.
BinaryFormatter formatter = new BinaryFormatter();
Ver01 oldVersion = new Ver01();
oldVersion.field1 = "Hello world!";
formatter.Serialize(memStream, oldVersion);
// Reset stream for reading
memStream.Position = 0;
// Deserialize the old version into a new instance.
Ver02 newVersion = (Ver02)formatter.Deserialize(memStream);
}
}
it into an existing serialization scheme. What I read from the docs
indicates that in the example below the .Deserialize() statement
should throw a SerializationException indicating that field2 is
expected from the data stream but Ver02.field2 is not marked with
OptionalFieldAttribute. Yet I get no such exception when I munge the
serialization of Ver01 a bit. Why?
--
Jeff S.
///////////////
[Serializable]
public class Ver01 : ISerializable {
public string field1;
public override GetObjectData(SerializationInfo info,
StreamingContext context) {
info.AssemblyName =
typeof(Ver02).Assembly.FullName.Replace("1.0.0.0", "0.0.0.9");
info.FullTypeName = typeof(Ver02).FullName;
info.AddValue("field1", this.field1);
}
}
[Serializable]
public class Ver02 {
public string field1;
public string field2;
}
public static class ExampleClass {
public static void TryDeserialize() {
MemoryStream memStream = new MemoryStream();
// Create an old instance and serialize it to a memory stream.
BinaryFormatter formatter = new BinaryFormatter();
Ver01 oldVersion = new Ver01();
oldVersion.field1 = "Hello world!";
formatter.Serialize(memStream, oldVersion);
// Reset stream for reading
memStream.Position = 0;
// Deserialize the old version into a new instance.
Ver02 newVersion = (Ver02)formatter.Deserialize(memStream);
}
}