Not getting expected SerializationException when missing OptionalFieldAttribute

Discussion in 'Microsoft Dot NET Framework' started by Jeff Stewart, Aug 29, 2007.

  1. Jeff Stewart

    Jeff Stewart Guest

    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);
    }
    }
     
    Jeff Stewart, Aug 29, 2007
    #1
    1. Advertisements

  2. Jeff Stewart

    Jeff Stewart Guest

    On Aug 29, 11:07 am, Jeff Stewart <> wrote:
    > 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.

    --
    Jeff S.
     
    Jeff Stewart, Aug 29, 2007
    #2
    1. Advertisements

  3. Jeff Stewart

    Jeff Stewart Guest

    On Aug 29, 1:05 pm, Jeff Stewart <> wrote:
    > On Aug 29, 11:07 am, Jeff Stewart <> wrote:
    >
    >
    >
    > > 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.
    >
    > --
    > Jeff S.


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

    --
    Jeff S.
     
    Jeff Stewart, Aug 29, 2007
    #3
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.
Similar Threads
  1. news.microsoft.com

    SerializationException

    news.microsoft.com, Jul 3, 2003, in forum: Microsoft Dot NET Framework
    Replies:
    5
    Views:
    887
    Derek Lakin
    Jul 3, 2003
  2. Matt

    System.Runtime.Serialization.SerializationExceptio n

    Matt, Jul 24, 2003, in forum: Microsoft Dot NET Framework
    Replies:
    0
    Views:
    768
  3. Matt Getting

    Re: System.Runtime.Serialization.SerializationExceptio n

    Matt Getting, Jul 25, 2003, in forum: Microsoft Dot NET Framework
    Replies:
    1
    Views:
    763
    Rob Teixeira [MVP]
    Jul 25, 2003
  4. Guest

    SerializationException during Deserialization of a class

    Guest, May 20, 2004, in forum: Microsoft Dot NET Framework
    Replies:
    3
    Views:
    586
    Guest
    May 25, 2004
  5. Jacek
    Replies:
    0
    Views:
    324
    Jacek
    Jun 12, 2004
  6. Guest

    System.Runtime.Serialization.SerializationExceptio n

    Guest, Dec 26, 2004, in forum: Microsoft Dot NET Framework
    Replies:
    2
    Views:
    779
    Richard Blewett [DevelopMentor]
    Dec 27, 2004
  7. Joan
    Replies:
    2
    Views:
    536
    Phil Wilson
    Jul 14, 2005
  8. Raghu

    OptionalFieldAttribute Experiments in .NET 2.0

    Raghu, Dec 22, 2005, in forum: Microsoft Dot NET Framework
    Replies:
    0
    Views:
    379
    Raghu
    Dec 22, 2005
Loading...