@@ -39,7 +39,7 @@ public static class BsonSerializer
3939 private static Dictionary < Type , IIdGenerator > __idGenerators = new Dictionary < Type , IIdGenerator > ( ) ;
4040 private static Dictionary < Type , IDiscriminatorConvention > __discriminatorConventions = new Dictionary < Type , IDiscriminatorConvention > ( ) ;
4141 private static Dictionary < BsonValue , HashSet < Type > > __discriminators = new Dictionary < BsonValue , HashSet < Type > > ( ) ;
42- private static HashSet < Type > __discriminatedTypes = new HashSet < Type > ( ) ;
42+ private static ConcurrentDictionary < Type , bool > __discriminatedTypes = new ( ) ;
4343 private static BsonSerializerRegistry __serializerRegistry ;
4444 private static TypeMappingSerializationProvider __typeMappingSerializationProvider ;
4545 // ConcurrentDictionary<Type, object> is being used as a concurrent set of Type. The values will always be null.
@@ -321,8 +321,7 @@ internal static bool IsDiscriminatorConventionRegisteredAtThisLevel(Type type)
321321 /// <returns>True if the type is discriminated.</returns>
322322 public static bool IsTypeDiscriminated ( Type type )
323323 {
324- var typeInfo = type . GetTypeInfo ( ) ;
325- return typeInfo . IsInterface || __discriminatedTypes . Contains ( type ) ;
324+ return type . IsInterface || __discriminatedTypes . ContainsKey ( type ) ;
326325 }
327326
328327 /// <summary>
@@ -587,7 +586,8 @@ public static void RegisterDiscriminator(Type type, BsonValue discriminator)
587586 // mark all base types as discriminated (so we know that it's worth reading a discriminator)
588587 for ( var baseType = typeInfo . BaseType ; baseType != null ; baseType = baseType . GetTypeInfo ( ) . BaseType )
589588 {
590- __discriminatedTypes . Add ( baseType ) ;
589+ // We expect that TryAdd will always return true, so no need to check the return value.
590+ __discriminatedTypes . TryAdd ( baseType , true ) ;
591591 }
592592 }
593593 }
0 commit comments