.../DictionaryGenericSerializerTests.cs | 37 ++++++++++++++++++++++ .../DictionaryInterfaceImplementerSerializer.cs | 5 +-- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/MongoDB.Bson.Tests/Serialization/Serializers/DictionaryGenericSerializerTests.cs b/src/MongoDB.Bson.Tests/Serialization/Serializers/DictionaryGenericSerializerTests.cs index f905778..20066e2 100644 --- a/src/MongoDB.Bson.Tests/Serialization/Serializers/DictionaryGenericSerializerTests.cs +++ b/src/MongoDB.Bson.Tests/Serialization/Serializers/DictionaryGenericSerializerTests.cs @@ -14,7 +14,9 @@ */ using System; +using System.Collections; using System.Collections.Generic; +using System.Collections.ObjectModel; using System.IO; using System.Linq; using System.Text; @@ -43,6 +45,19 @@ namespace MongoDB.Bson.Tests.Serialization.DictionaryGenericSerializers public SortedList SL { get; set; } } + public class RO : ReadOnlyDictionary + { + private RO(IDictionary dictionary) + : base(dictionary) + { + } + + public static RO ConstructorReplacement(IDictionary dictionary) + { + return new RO(dictionary); + } + } + [Test] public void TestNull() { @@ -388,6 +403,28 @@ namespace MongoDB.Bson.Tests.Serialization.DictionaryGenericSerializers Assert.Throws(() => obj.ToBson()); } + [Test] + public void TestImmutablePrivateConstructorDictionaryImplementation() + { + var d = new Dictionary { { "A", new C { P = "x" } } }; + var id = RO.ConstructorReplacement(d); + var sd = CreateSortedDictionary(d); + var sl = CreateSortedList(d); + var obj = new T { D = d, ID = id, SD = sd, SL = sl }; + var json = obj.ToJson(); + var rep = "{ 'A' : { '_t' : 'DictionaryGenericSerializers.C', 'P' : 'x' } }"; + var expected = "{ 'D' : #R, 'ID' : #R, 'SD' : #R, 'SL' : #R }".Replace("#R", rep).Replace("'", "\""); + Assert.AreEqual(expected, json); + + var bson = obj.ToBson(); + var rehydrated = BsonSerializer.Deserialize(bson); + Assert.IsInstanceOf>(rehydrated.D); + Assert.IsInstanceOf>(rehydrated.ID); + Assert.IsInstanceOf>(rehydrated.SD); + Assert.IsInstanceOf>(rehydrated.SL); + Assert.IsTrue(bson.SequenceEqual(rehydrated.ToBson())); + } + private SortedDictionary CreateSortedDictionary(Dictionary d) { var sd = new SortedDictionary(); diff --git a/src/MongoDB.Bson/Serialization/Serializers/DictionaryInterfaceImplementerSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/DictionaryInterfaceImplementerSerializer.cs index 7d91011..f923671 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/DictionaryInterfaceImplementerSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/DictionaryInterfaceImplementerSerializer.cs @@ -13,6 +13,7 @@ * limitations under the License. */ +using System; using System.Collections; using System.Collections.Generic; using MongoDB.Bson.Serialization.Options; @@ -164,7 +165,7 @@ namespace MongoDB.Bson.Serialization.Serializers DictionarySerializerBase, IChildSerializerConfigurable, IDictionaryRepresentationConfigurable> - where TDictionary : class, IDictionary, new() + where TDictionary : class, IDictionary { /// /// Initializes a new instance of the class. @@ -271,7 +272,7 @@ namespace MongoDB.Bson.Serialization.Serializers /// The instance. protected override TDictionary CreateInstance() { - return new TDictionary(); + return Activator.CreateInstance(); } // explicit interface implementations