-
Type: Bug
-
Resolution: Done
-
Priority: Minor - P4
-
Affects Version/s: 2.0, 2.0.1
-
Component/s: BSON, Serialization
When attempting to serialize a property of IDictionary<T,U>, if the implementing class does not contain a public parameterless constructor then serialization will fail with exception:
System.ArgumentException: GenericArguments[0], 'Microsoft.FSharp.Core.ExtraTopLevelOperators+CreateDictionary@45[System.String,System.DateTime]', on 'MongoDB.Bson.Serialization.Serializers.DictionaryInterfaceImplementerSerializer`3[TDictionary,TKey,TValue]' violates the constraint of type 'TDictionary'. ---> System.TypeLoadException: GenericArguments[0], 'Microsoft.FSharp.Core.ExtraTopLevelOperators+CreateDictionary@45[System.String,System.DateTime]', on 'MongoDB.Bson.Serialization.Serializers.DictionaryInterfaceImplementerSerializer`3[TDictionary,TKey,TValue]' violates the constraint of type parameter 'TDictionary'.
Example above (stack trace attached) from setting property as return of dict operator in F# Core https://msdn.microsoft.com/en-us/library/ee353774.aspx the implementation of which is the result of an object expression and therefore does not contain a public constructor.
Attached is a unit test that works correctly in 1.x but does not work in 2.x (non F# but same principle).
Exception is resolvable by changing signature of class DictionaryInterfaceImplementerSerializer<TDictionary, TKey, TValue> to remove the new() constraint, and then changing implementation of DictionaryInterfaceImplementerSerializer<TDictionary, TKey, TValue>.CreateInstance() to return Activator.CreateInstance<TDictionary>()
- related to
-
CSHARP-2154 Support IReadOnlyDictionary/ReadOnlyDictionary serialization
- Closed