Bug 9526 - [patch] BinaryFormatter cannot deserialize ObservableCollection<> instances serialized with .net
Summary: [patch] BinaryFormatter cannot deserialize ObservableCollection<> instances s...
Status: RESOLVED FIXED
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib ()
Version: master
Hardware: All All
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2013-01-13 20:15 UTC by David McFarland
Modified: 2015-03-20 06:08 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
patch (8.72 KB, patch)
2013-01-13 20:15 UTC, David McFarland
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description David McFarland 2013-01-13 20:15:16 UTC
Created attachment 3206 [details]
patch

I've hit a few problems trying to load an instance of ObservableCollection<T> serialized from .net with BinaryFormatter.

I've attached my local patch, and I'll walk through the changes:


1) WindowsBase assembly version mismatch

The first problem I hit was that the serialized data specified WindowsBase version 3.0.0.0, but I'm running on profile 4.5, which has version 4.0.0.0.  System assemblies are normally mapped to the current framework version, but WindowsBase wasn't in the list, so it was trying to load the 3.0.0.0 assembly for serialization.

My patch adds WindowsBase to the framework_assemblies list in mono/metadata/assembly.c.


2) ObjectReader fails to properly parse objects with missing members

ObjectReader would only read values from the stream if the field could be found on the type, or if the type was primitive.  In the case of a non-primitive missing field, the stream would enter an inconsistent state.

My patch to mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectReader.cs makes ObjectReader always read a value and just throw it away if there is no field.


3) The serialized structure of ObservableCollection<T> doesn't match .net

An BinaryFormatter serialized instance of the .net implementation of ObservableCollection<T> has data for two fields:

ObservableCollection<T>+SimpleMonitor _monitor
List<T> Collection<T>+items

The mono implementation of Collection<T> has a field called 'list', but not 'items', so the contents of the collection will not be serialized.

My patch adds an empty 'SimpleMonitor' class, and renames 'list' to 'items' in Collection<T>.  This will break compatibility with data serialized in earlier versions of mono, but will improve compatibility with .net.  I'm sure a hack could be added to read the old 'list' field, but I'm not sure where would be best.


*I haven't yet tested serialization from mono or its compatibility with .net going the other way.  I also haven't dealt with the other fields in ObservableCollection such as _monitor in .net or Collection<T>.syncRoot in mono.
Comment 1 Marek Safar 2015-03-20 06:08:49 UTC
Fixed in master