@@ -43,6 +43,7 @@ public static class BsonSerializer
43
43
// static constructor
44
44
static BsonSerializer ( )
45
45
{
46
+ RegisterDefaultSerializationProvider ( ) ;
46
47
RegisterIdGenerators ( ) ;
47
48
}
48
49
@@ -358,16 +359,22 @@ public static IIdGenerator LookupIdGenerator(Type type)
358
359
}
359
360
360
361
/// <summary>
361
- /// Looks up and populates the serializer singleton for a type .
362
+ /// Looks up a serializer for a Type .
362
363
/// </summary>
363
364
/// <param name="type">The Type.</param>
364
365
/// <returns>A serializer for the Type.</returns>
365
366
public static IBsonSerializer LookupSerializer ( Type type )
366
367
{
368
+ // since we don't allow registering serializers for classes that implement IBsonSerializable no lookup is needed
369
+ if ( typeof ( IBsonSerializable ) . IsAssignableFrom ( type ) )
370
+ {
371
+ return Serializers . BsonIBsonSerializableSerializer . Instance ;
372
+ }
373
+
367
374
__configLock . EnterReadLock ( ) ;
368
375
try
369
376
{
370
- IBsonSerializer serializer ;
377
+ IBsonSerializer serializer ;
371
378
if ( __serializers . TryGetValue ( type , out serializer ) )
372
379
{
373
380
return serializer ;
@@ -384,24 +391,6 @@ public static IBsonSerializer LookupSerializer(Type type)
384
391
IBsonSerializer serializer ;
385
392
if ( ! __serializers . TryGetValue ( type , out serializer ) )
386
393
{
387
- if ( serializer == null )
388
- {
389
- foreach ( var serializationProvider in __serializationProviders )
390
- {
391
- serializer = serializationProvider . GetSerializer ( type ) ;
392
- if ( serializer != null )
393
- {
394
- break ;
395
- }
396
- }
397
- }
398
-
399
- // special case for IBsonSerializable
400
- if ( serializer == null && typeof ( IBsonSerializable ) . IsAssignableFrom ( type ) )
401
- {
402
- serializer = Serializers . BsonIBsonSerializableSerializer . Instance ;
403
- }
404
-
405
394
if ( serializer == null && type . IsGenericType )
406
395
{
407
396
var genericTypeDefinition = type . GetGenericTypeDefinition ( ) ;
@@ -415,7 +404,14 @@ public static IBsonSerializer LookupSerializer(Type type)
415
404
416
405
if ( serializer == null )
417
406
{
418
- serializer = BsonDefaultSerializer . Instance . GetSerializer ( type ) ;
407
+ foreach ( var serializationProvider in __serializationProviders )
408
+ {
409
+ serializer = serializationProvider . GetSerializer ( type ) ;
410
+ if ( serializer != null )
411
+ {
412
+ break ;
413
+ }
414
+ }
419
415
}
420
416
421
417
if ( serializer == null )
@@ -479,11 +475,6 @@ public static void RegisterIdGenerator(Type type, IIdGenerator idGenerator)
479
475
/// <param name="provider">The serialization provider.</param>
480
476
public static void RegisterSerializationProvider ( IBsonSerializationProvider provider )
481
477
{
482
- if ( provider == BsonDefaultSerializer . Instance )
483
- {
484
- throw new ArgumentException ( "BsonDefaultSerializer is implicitly registered" , "provider" ) ;
485
- }
486
-
487
478
__configLock . EnterWriteLock ( ) ;
488
479
try
489
480
{
@@ -503,6 +494,12 @@ public static void RegisterSerializationProvider(IBsonSerializationProvider prov
503
494
/// <param name="serializer">The serializer.</param>
504
495
public static void RegisterSerializer ( Type type , IBsonSerializer serializer )
505
496
{
497
+ if ( typeof ( IBsonSerializable ) . IsAssignableFrom ( type ) )
498
+ {
499
+ var message = string . Format ( "A serializer cannot be registered for type {0} because it implements IBsonSerializable." , BsonUtils . GetFriendlyTypeName ( type ) ) ;
500
+ throw new BsonSerializationException ( message ) ;
501
+ }
502
+
506
503
__configLock . EnterWriteLock ( ) ;
507
504
try
508
505
{
@@ -569,12 +566,25 @@ public static void Serialize(
569
566
object value ,
570
567
IBsonSerializationOptions options )
571
568
{
569
+ // since we don't allow registering serializers for classes that implement IBsonSerializable no lookup is needed
570
+ var bsonSerializable = value as IBsonSerializable ;
571
+ if ( bsonSerializable != null )
572
+ {
573
+ bsonSerializable . Serialize ( bsonWriter , nominalType , options ) ;
574
+ return ;
575
+ }
576
+
572
577
var actualType = ( value == null ) ? nominalType : value . GetType ( ) ;
573
578
var serializer = LookupSerializer ( actualType ) ;
574
579
serializer . Serialize ( bsonWriter , nominalType , value , options ) ;
575
580
}
576
581
577
582
// private static methods
583
+ private static void RegisterDefaultSerializationProvider ( )
584
+ {
585
+ RegisterSerializationProvider ( BsonDefaultSerializer . Instance ) ;
586
+ }
587
+
578
588
private static void RegisterIdGenerators ( )
579
589
{
580
590
BsonSerializer . RegisterIdGenerator ( typeof ( BsonObjectId ) , BsonObjectIdGenerator . Instance ) ;
0 commit comments