Bug 7299 - DataContractSerializer cannot deserialize ReadOnlyCollection<T>
Summary: DataContractSerializer cannot deserialize ReadOnlyCollection<T>
Alias: None
Product: Class Libraries
Classification: Mono
Component: WCF assemblies ()
Version: master
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Martin Baulig
Depends on:
Reported: 2012-09-19 11:23 UTC by Jonathan Pryor
Modified: 2016-11-11 09:30 UTC (History)
2 users (show)

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

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:

Description Jonathan Pryor 2012-09-19 11:23:35 UTC
DataContractSerializer cannot deserialize ReadOnlyCollection<T>.

Example program:

  using System;
  using System.Collections.ObjectModel;
  using System.IO;
  using System.Linq;
  using System.Runtime.Serialization;

  class Test {
    public static void Main ()
      var list = new ReadOnlyCollection<int>(new[]{1,2,3});
      var s = new DataContractSerializer(typeof(ReadOnlyCollection<int>));
      var o = new MemoryStream ();
      s.WriteObject(o, list); 
      var list2 = (ReadOnlyCollection<int>) s.ReadObject (o);
      Console.WriteLine ("done: {0}",
          string.Join (" ", list2.Select (i => i)));

Build & run on .NET:

  Z:\tmp\csharp>csc roc.cs /r:System.Runtime.Serialization.dll
  Microsoft (R) Visual C# Compiler version 4.0.30319.17626
  for Microsoft (R) .NET Framework 4.5
  Copyright (C) Microsoft Corporation. All rights reserved.

  done: 1 2 3

Build & Run on mono/2.11.3 (master/876615e Thu Aug  9 15:59:47 EDT 2012):

> $ dmcs roc.cs -r:System.Runtime.Serialization.dll && mono roc.exe 
> Unhandled Exception:
> System.MissingMethodException: Default constructor not found for type System.Collections.ObjectModel.ReadOnlyCollection`1[[System.Int32, mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089]].
>   at System.Activator.CreateInstance (System.Type type, Boolean nonPublic) [0x00000] in <filename unknown>:0 
>   at System.Runtime.Serialization.CollectionTypeMap.CreateInstance () [0x00000] in <filename unknown>:0 
>   at System.Runtime.Serialization.CollectionTypeMap.DeserializeContent (System.Xml.XmlReader reader, System.Runtime.Serialization.XmlFormatterDeserializer deserializer, System.String id) [0x00000] in <filename unknown>:0 
>   at System.Runtime.Serialization.SerializationMap.DeserializeObject (System.Xml.XmlReader reader, System.Runtime.Serialization.XmlFormatterDeserializer deserializer) [0x00000] in <filename unknown>:0 
>   at System.Runtime.Serialization.XmlFormatterDeserializer.DeserializeByMap (System.Xml.XmlQualifiedName name, System.Type type, System.Xml.XmlReader reader) [0x00000] in <filename unknown>:0 
>   at System.Runtime.Serialization.XmlFormatterDeserializer.Deserialize (System.Type type, System.Xml.XmlReader reader) [0x00000] in <filename unknown>:0 
>   at System.Runtime.Serialization.XmlFormatterDeserializer.Deserialize (System.Xml.XmlReader reader, System.Type declaredType, System.Runtime.Serialization.KnownTypeCollection knownTypes, IDataContractSurrogate surrogate, System.Runtime.Serialization.DataContractResolver resolver, System.Runtime.Serialization.DataContractResolver defaultResolver, System.String name, System.String ns, Boolean verifyObjectName) [0x00000] in <filename unknown>:0 
>   at System.Runtime.Serialization.DataContractSerializer.ReadObject (System.Xml.XmlDictionaryReader reader, Boolean verifyObjectName) [0x00000] in <filename unknown>:0 
>   at System.Runtime.Serialization.XmlObjectSerializer.ReadObject (System.Xml.XmlDictionaryReader reader) [0x00000] in <filename unknown>:0 
>   at System.Runtime.Serialization.DataContractSerializer.ReadObject (System.Xml.XmlReader reader) [0x00000] in <filename unknown>:0 
>   at System.Runtime.Serialization.XmlObjectSerializer.ReadObject (System.IO.Stream stream) [0x00000] in <filename unknown>:0 
>   at Test.Main () [0x00000] in <filename unknown>:0
Comment 1 Martin Baulig 2012-09-20 00:44:35 UTC
Looks like .NET is using special "magic" here to handle this by treating it as an IList<T> on the wire.  There's actually some comment about this particular type in DataContractSerializer's MSDN docs.

Wondering whether there are more such types (other readonly collection types maybe?).
Comment 3 Martin Baulig 2012-09-24 20:27:23 UTC
First part of this is now fixed in master/f7e1f8a.  This commit fixes the ReadOnlyCollection<T> as well of a bunch of other problems and added several new nunit tests.

I still need to look at [CollectionDataContract] and dictionary collections.
Comment 4 Martin Baulig 2012-09-24 21:25:19 UTC
Forgot to actually push this upstream, the real commit number is master/e5e80ee.