-
Type:
Bug
-
Resolution: Works as Designed
-
Priority:
Minor - P4
-
None
-
Affects Version/s: 2.19.0
-
Component/s: None
-
None
-
None
-
None
-
None
-
None
-
None
-
None
-
None
In version 2.18 (and before) we used a property attribute to define a custom dictionary serializer as below:
[BsonSerializer(typeof(CustomDictionarySerializer<Dictionary<AccountConfigType, string>, EnumStringSerializer<AccountConfigType>, CustomObjectSerializer<string>>))]
But after upgrading to 2.19, we would get the error:
System.TypeInitializationException : The type initializer for 'i3vDB.Collections.Account' threw an exception.
----> System.ArgumentException : Value type of serializer is System.Collections.Generic.Dictionary`2[[Lib.Shared.Enum.AccountConfigType, Lib.Shared, Version=2.1.4.1, Culture=neutral, PublicKeyToken=null],[System.String, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]] and does not match member type System.Collections.Generic.Dictionary`2[[Lib.Shared.Enum.AccountConfigType, "Lib.Shared ", Version=2.1.4.1, Culture=neutral, PublicKeyToken=null],[System.String, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]. (Parameter 'serializer')
Removing the property attribute, and instead using the below fixes the issue.
BsonSerializer.RegisterGenericSerializerDefinition(typeof(Dictionary<AccountConfigType, string>), typeof(CustomDictionarySerializer<Dictionary<AccountConfigType, string>, EnumStringSerializer<AccountConfigType>, CustomObjectSerializer<string>>));
I should also note that if you have both the property attribute and the explicit registration, it throws the same exception. I did not test any other custom serializers as we didn't have any others specified by property attribute.
I don't know if it's significant as it could just be caused by the exception output, but the TYPE X doesn't match TYPE X message only differs in Lib.Shared. The second instance is in quotes and has a space at the end. They are otherwise identical.