[Mono-bugs] [Bug 552891] New: Mono can't deserialize Nullable objects created by .NET

bugzilla_noreply at novell.com bugzilla_noreply at novell.com
Thu Nov 5 11:02:15 EST 2009


           Summary: Mono can't deserialize Nullable objects created by
    Classification: Mono
           Product: Mono: Runtime
           Version: 2.4.x
          Platform: x86
        OS/Version: All
            Status: NEW
          Severity: Normal
          Priority: P5 - None
         Component: remoting
        AssignedTo: lluis at novell.com
        ReportedBy: jorge.matias at fractaliasoftware.com
         QAContact: mono-bugs at lists.ximian.com
          Found By: ---

User-Agent:       Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US)
AppleWebKit/532.0 (KHTML, like Gecko) Chrome/ Safari/532.0

Mono can't deserialize classes that include any kind of Nullable object when
they have been serialized with .NET and they have been assigned a value which
is not null.

Sample code:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

using NUnit.Framework;

namespace TestSerialization
    class SimpleClass
        string id;
        DateTime? nullableTimeStamp;
        int? nullableInt;

        public string Id
            get { return id; }
            set { id = value; }

        public DateTime? NullableTimeStamp
            get { return nullableTimeStamp; }
            set { nullableTimeStamp = value; }

        public int? NullableInt
            get { return nullableInt; }
            set { nullableInt = value; }

        public override bool Equals(object obj)
                SimpleClass c = (SimpleClass)obj;
                return id.Equals(c.id) 
                    && nullableTimeStamp.Equals(c.nullableTimeStamp)
                    && nullableInt.Equals(c.nullableInt);
            catch (Exception ex)
                return false;

    public class NullableSerializationTest
        SimpleClass c;

        public NullableSerializationTest()
            c = new SimpleClass();
            c.Id = "123456";
            c.NullableTimeStamp = null;
            c.NullableInt = null;

        void SerializeObject(Object o, string file)
            Stream stream = File.Create(file);
            BinaryFormatter oFormatter = new BinaryFormatter();
            oFormatter.Serialize(stream, o);

        Object DeserializeObject(string file)
            Stream stream = File.OpenRead(file);
            BinaryFormatter oFormatter = new BinaryFormatter();
            Object o = oFormatter.Deserialize(stream);
            return o;

        public void TestSerialization()
            SerializeObject(c, "simpleobj.bin");

        public void TestDeserialization()
            SimpleClass c2 = (SimpleClass) DeserializeObject("simpleobj.bin");
            Assert.AreEqual(c, c2);

Reproducible: Always

Steps to Reproduce:
1. Change the following lines:
 a) Replace 
      c.NullableTimeStamp = null; 
      c.NullableTimeStamp = DateTime.Parse("1/1/2009");
 b) Replace
      c.NullableInt = null;
      c.NullableInt = 10;

2. Run TestSerialization from .NET 2.0 or 3.5 (I tested both).

3. Run TestDeserialization from Mono 2.4 (tested on Windows, OpenSUSE and Mac
OS X).

Actual Results:  
1.a (after changing the Nullable DateTime value):

TestCase 'TestSerialization.NullableSerializationTest.TestDeserialization'
failed: System.ArgumentOutOfRangeException : Value 4159925407799315720 is
outside the valid range [0,3155378975999999999].
Parameter name: ticks
    at System.DateTime..ctor (Int64 ticks) [0x00000]
    at System.DateTime..ctor (Int64 ticks, DateTimeKind kind) [0x00000]
    at System.DateTime.FromBinary (Int64 dateData) [0x00000]
(System.IO.BinaryReader reader, System.Type type) [0x00000]

1.b (after changing the Nullable int value):

TestCase 'TestSerialization.NullableSerializationTest.TestDeserialization'
failed: System.Runtime.Serialization.SerializationException : Unexpected binary
element: 0
    at System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadObject
(BinaryElement element, System.IO.BinaryReader reader, System.Int64& objectId,
System.Object& value, System.Runtime.Serialization.SerializationInfo& info)
(System.IO.BinaryReader reader) [0x00000]

Expected Results:  
The object is deserialized correctly and the test is succesfully run for every

The exceptions don't happen if:
- Both objects are set to null.
- The Nullable part of the object definitions is removed, that is, the objects
are defined as DateTime or int or whatever.

BTW, do you guys know how could a workaround be developed for this problem with
a custom SurrogateSelector? Thanks!

Configure bugmail: http://bugzilla.novell.com/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.

More information about the mono-bugs mailing list