Not getting expected SerializationException when missing OptionalFieldAttribute

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);
}
}
 
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);
}

}

Seems I found an answer:
http://www.tech-archive.net/pdf/Arc...lic.dotnet.framework.clr/2006-07/msg00024.pdf

OptionalFieldAttribute isn't required in .NET 2.0 unless
BinaryFormatter.AssemblyFormat == FormatterAssemblyStyle.Simple.
 
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);
}

Seems I found an answer:http://www.tech-archive.net/pdf/Archive/DotNet/microsoft.public.dotne...

OptionalFieldAttribute isn't required in .NET 2.0 unless
BinaryFormatter.AssemblyFormat == FormatterAssemblyStyle.Simple.

Sorry, isn't required in .NET 2.0 *if* AssemblyFormat == Simple (the
default in .NET 2.0).
 

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

Top