1717using System . Collections ;
1818using System . Collections . Generic ;
1919using System . Linq . Expressions ;
20- using System . Reflection ;
2120using MongoDB . Bson ;
2221using MongoDB . Bson . Serialization ;
2322using MongoDB . Bson . Serialization . Options ;
2423using MongoDB . Bson . Serialization . Serializers ;
2524using MongoDB . Driver . Linq . Linq3Implementation . Misc ;
26- using MongoDB . Driver . Support ;
25+ using MongoDB . Driver . Linq . Linq3Implementation . Serializers ;
2726
2827namespace MongoDB . Driver . Linq . Linq3Implementation . KnownSerializerFinders ;
2928
@@ -50,6 +49,8 @@ protected override Expression VisitMember(MemberExpression node)
5049 _ when declaringType == typeof ( BsonValue ) => GetBsonValuePropertySerializer ( ) ,
5150 _ when IsCollectionCountOrLengthProperty ( ) => GetCollectionCountOrLengthPropertySerializer ( ) ,
5251 _ when declaringType == typeof ( DateTime ) => GetDateTimePropertySerializer ( ) ,
52+ _ when declaringType . IsConstructedGenericType && declaringType . GetGenericTypeDefinition ( ) == typeof ( Dictionary < , > ) => GetDictionaryPropertySerializer ( ) ,
53+ _ when declaringType . IsConstructedGenericType && declaringType . GetGenericTypeDefinition ( ) == typeof ( IDictionary < , > ) => GetIDictionaryPropertySerializer ( ) ,
5354 _ when declaringType . IsNullable ( ) => GetNullablePropertySerializer ( ) ,
5455 _ when IsTupleOrValueTuple ( declaringType ) => GetTupleOrValueTuplePropertySerializer ( ) ,
5556 _ => GetPropertySerializer ( )
@@ -136,6 +137,42 @@ IBsonSerializer GetDateTimePropertySerializer()
136137 } ;
137138 }
138139
140+ IBsonSerializer GetDictionaryPropertySerializer ( )
141+ {
142+ if ( containingSerializer . Unwrapped ( ) is not IBsonDictionarySerializer dictionarySerializer )
143+ {
144+ throw new ExpressionNotSupportedException ( node , because : "DictionarySerializer does not implement IBsonDictionarySerializer" ) ;
145+ }
146+
147+ var keySerializer = dictionarySerializer . KeySerializer ;
148+ var valueSerializer = dictionarySerializer . ValueSerializer ;
149+
150+ return memberName switch
151+ {
152+ "Keys" => DictionaryKeyCollectionSerializer . Create ( keySerializer , valueSerializer ) ,
153+ "Values" => DictionaryValueCollectionSerializer . Create ( keySerializer , valueSerializer ) ,
154+ _ => throw new ExpressionNotSupportedException ( node , because : $ "Unexpected member name: { memberName } ")
155+ } ;
156+ }
157+
158+ IBsonSerializer GetIDictionaryPropertySerializer ( )
159+ {
160+ if ( containingSerializer is not IBsonDictionarySerializer dictionarySerializer )
161+ {
162+ throw new ExpressionNotSupportedException ( node , because : "IDictionarySerializer does not implement IBsonDictionarySerializer" ) ;
163+ }
164+
165+ var keySerializer = dictionarySerializer . KeySerializer ;
166+ var valueSerializer = dictionarySerializer . ValueSerializer ;
167+
168+ return memberName switch
169+ {
170+ "Keys" => ICollectionSerializer . Create ( keySerializer ) ,
171+ "Values" => ICollectionSerializer . Create ( valueSerializer ) ,
172+ _ => throw new ExpressionNotSupportedException ( node , because : $ "Unexpected member name: { memberName } ")
173+ } ;
174+ }
175+
139176 IBsonSerializer GetNullablePropertySerializer ( )
140177 {
141178 return memberName switch
0 commit comments