Bug 567522 - BinaryFormatter doesn't deserialize objects with nullable types
Summary: BinaryFormatter doesn't deserialize objects with nullable types
Status: RESOLVED DUPLICATE of bug 552891
Alias: None
Product: Mono: Class Libraries
Classification: Mono
Component: CORLIB (show other bugs)
Version: 2.6.x
Hardware: x86 Other
: P5 - None : Normal
Target Milestone: ---
Assignee: Mono Bugs
QA Contact: Mono Bugs
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-12-29 11:15 UTC by Mika Aalto
Modified: 2010-09-13 12:01 UTC (History)
1 user (show)

See Also:
Found By: ---
Services Priority:
Business Priority:
Blocker: ---
Marketing QA Status: ---
IT Deployment: ---


Attachments
Testing application (2.95 KB, application/zip)
2009-12-29 11:15 UTC, Mika Aalto
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Mika Aalto 2009-12-29 11:15:30 UTC
Created attachment 334470 [details]
Testing application

User-Agent:       Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.6) Gecko/20091216 Mandriva Linux/1.9.1.6-0.1mdv2010.0 (2010.0) Firefox/3.5.6

Object serialized in MS .NET environment using BinaryFormatter can't be deserialized with mono.

Attached example application contains data.bin file which is object serialized in MS .NET. Running attached application deserializes this file correctly with MS .NET but not with mono.

Reproducible: Always

Steps to Reproduce:
1. Run attached example application
2.
3.
Actual Results:  
Unhandled Exception: 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) [0x00000] in <filename unknown>:0
  at System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadValue (System.IO.BinaryReader reader, System.Object parentObject, Int64 parentObjectId, System.Runtime.Serialization.SerializationInfo info, System.Type valueType, System.String fieldName, System.Reflection.MemberInfo memberInfo, System.Int32[] indices) [0x00000] in <filename unknown>:0
  at System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadObjectContent (System.IO.BinaryReader reader, System.Runtime.Serialization.Formatters.Binary.TypeMetadata metadata, Int64 objectId, System.Object& objectInstance, System.Runtime.Serialization.SerializationInfo& info) [0x00000] in <filename unknown>:0
  at System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadObjectInstance (System.IO.BinaryReader reader, Boolean isRuntimeObject, Boolean hasTypeInfo, System.Int64& objectId, System.Object& value, System.Runtime.Serialization.SerializationInfo& info) [0x00000] in <filename unknown>: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) [0x00000] in <filename unknown>: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) [0x00000] in <filename unknown>:0
  at System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadNextObject (BinaryElement element, System.IO.BinaryReader reader) [0x00000] in <filename unknown>:0
  at System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadObjectGraph (BinaryElement elem, System.IO.BinaryReader reader, Boolean readHeaders, System.Object& result, System.Runtime.Remoting.Messaging.Header[]& headers) [0x00000] in <filename unknown>:0
  at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.NoCheckDeserialize (System.IO.Stream serializationStream, System.Runtime.Remoting.Messaging.HeaderHandler handler) [0x00000] in <filename unknown>:0
  at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize (System.IO.Stream serializationStream) [0x00000] in <filename unknown>:0
  at SerializationTest.MainClass.Main (System.String[] args) [0x00000] in <filename unknown>:0

Expected Results:  
Class deserialized
Comment 1 Forgotten User oDRaEXi7Ku 2010-09-09 11:30:12 UTC
Hello Mika, the testcase is incomplete, could you share also the source code to generate the bin file that you bundled in it? Thanks!
Comment 2 Forgotten User oDRaEXi7Ku 2010-09-09 16:09:38 UTC
I deduced the initialization of the object, it's:

            AddressBookContact abc = new AddressBookContact();
            abc.Id = 0;
            abc.FirstName = "Unit";
            abc.LastName = "Testing";
            abc.State = ContactState.Unknown;
            abc.PrimaryPhoneNumber = "0405087124";
            abc.MobilePhoneNumber = "050123456789";
            abc.HomePhoneNumber = "+3589123456";
            abc.SecondaryMobilePhoneNumber = "";
            abc.FaxNumber = "123";
            abc.EmailAddress = "unit.test@ccxtech.fi";
            abc.OrganizationId = 1;
            abc.Title = "Tester";
            abc.UserId = 1;
            abc.ContactGroupIds = new List<int>();

And I confirm it fails in master.

I'll try to isolate the testcase.
Comment 3 Forgotten User oDRaEXi7Ku 2010-09-09 16:46:36 UTC
I have isolated the problem to just nullable types.

This simple class fails to be deserialized on mono when assigning to the field the value 1:

[Serializable]
public class AddressBookContact
{
  public int? UserId;
}
Comment 4 Forgotten User oDRaEXi7Ku 2010-09-13 12:01:53 UTC
Actually it's a dupe, this all sounded very familiar to me...

*** This bug has been marked as a duplicate of bug 552891 ***