using System.Collections; using System.Collections.Generic; using MongoDB.Bson; using MongoDB.Bson.Serialization; using NUnit.Framework; namespace MongoTest { [TestFixture] public class MongoDictionarySerializationTest { public class Dto { public IDictionary Dictionary { get; set; } } public class ConstructorLessDictionary : IDictionary { private readonly IDictionary _inner; private ConstructorLessDictionary() { _inner = new Dictionary(); } public static ConstructorLessDictionary ConstructorReplacement() { return new ConstructorLessDictionary(); } public TValue this[TKey key] { get { return _inner[key]; } set { _inner[key] = value; } } public int Count { get { return _inner.Count; } } public bool IsReadOnly { get { return _inner.IsReadOnly; } } public ICollection Keys { get { return _inner.Keys; } } public ICollection Values { get { return _inner.Values; } } public void Add(KeyValuePair item) { _inner.Add(item); } public void Add(TKey key, TValue value) { _inner.Add(key, value); } public void Clear() { _inner.Clear(); } public bool Contains(KeyValuePair item) { return _inner.Contains(item); } public bool ContainsKey(TKey key) { return _inner.ContainsKey(key); } public void CopyTo(KeyValuePair[] array, int arrayIndex) { _inner.CopyTo(array, arrayIndex); } public IEnumerator> GetEnumerator() { return _inner.GetEnumerator(); } public bool Remove(KeyValuePair item) { return _inner.Remove(item); } public bool Remove(TKey key) { return _inner.Remove(key); } public bool TryGetValue(TKey key, out TValue value) { return _inner.TryGetValue(key, out value); } IEnumerator IEnumerable.GetEnumerator() { return ((IEnumerable)_inner).GetEnumerator(); } } [Test] public void TestNoConstructorDictionarySerialization() { var dto = new Dto { Dictionary = ConstructorLessDictionary.ConstructorReplacement() }; BsonClassMap.RegisterClassMap(cm => cm.AutoMap()); Assert.DoesNotThrow(() => dto.ToBsonDocument()); } } }