@@ -39,7 +39,7 @@ public static class BsonSerializer
39
39
private static Dictionary < Type , IIdGenerator > __idGenerators = new Dictionary < Type , IIdGenerator > ( ) ;
40
40
private static Dictionary < Type , IDiscriminatorConvention > __discriminatorConventions = new Dictionary < Type , IDiscriminatorConvention > ( ) ;
41
41
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 ( ) ;
43
43
private static BsonSerializerRegistry __serializerRegistry ;
44
44
private static TypeMappingSerializationProvider __typeMappingSerializationProvider ;
45
45
// 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)
321
321
/// <returns>True if the type is discriminated.</returns>
322
322
public static bool IsTypeDiscriminated ( Type type )
323
323
{
324
- var typeInfo = type . GetTypeInfo ( ) ;
325
- return typeInfo . IsInterface || __discriminatedTypes . Contains ( type ) ;
324
+ return type . IsInterface || __discriminatedTypes . ContainsKey ( type ) ;
326
325
}
327
326
328
327
/// <summary>
@@ -587,7 +586,8 @@ public static void RegisterDiscriminator(Type type, BsonValue discriminator)
587
586
// mark all base types as discriminated (so we know that it's worth reading a discriminator)
588
587
for ( var baseType = typeInfo . BaseType ; baseType != null ; baseType = baseType . GetTypeInfo ( ) . BaseType )
589
588
{
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 ) ;
591
591
}
592
592
}
593
593
}
0 commit comments