@@ -439,16 +439,12 @@ static internal bool IsNonAttributedTypeValidForSerialization(Type type)
439
439
}
440
440
}
441
441
442
- private static string [ ] s_knownSerializableTypeNames = new string [ ] {
443
- "System.Collections.Queue" ,
444
- "System.Collections.Stack" ,
445
- "System.Globalization.CultureInfo" ,
446
- "System.Version" ,
442
+ private static string [ ] s_knownSerializableGenericTypeNames = new string [ ] {
447
443
"System.Collections.Generic.KeyValuePair`2" ,
448
- "System.Collections.Generic.Queue`1" ,
449
- "System.Collections.Generic.Stack`1" ,
450
- "System.Collections.ObjectModel.ReadOnlyCollection`1" ,
451
- "System.Collections.ObjectModel.ReadOnlyDictionary`2" ,
444
+ "System.Collections.Generic.Queue`1;_syncRoot " ,
445
+ "System.Collections.Generic.Stack`1;_syncRoot " ,
446
+ "System.Collections.ObjectModel.ReadOnlyCollection`1;_syncRoot " ,
447
+ "System.Collections.ObjectModel.ReadOnlyDictionary`2;_syncRoot,_keys,_values " ,
452
448
"System.Tuple`1" ,
453
449
"System.Tuple`2" ,
454
450
"System.Tuple`3" ,
@@ -459,26 +455,106 @@ static internal bool IsNonAttributedTypeValidForSerialization(Type type)
459
455
"System.Tuple`8" ,
460
456
} ;
461
457
458
+ private static string [ ] s_knownSerializableNonGenericTypeNames = new string [ ] {
459
+ "System.Collections.Queue;_syncRoot" ,
460
+ "System.Collections.Stack;_syncRoot" ,
461
+ "System.Globalization.CultureInfo" ,
462
+ "System.Version" ,
463
+ } ;
464
+
465
+ private static object s_knownSerializableGenericTypeInfosLock = new object ( ) ;
466
+ private static volatile IDictionary < string , SerializableInfo > s_knownSerializableGenericTypeInfos ;
467
+ private static IDictionary < string , SerializableInfo > KnownSerializableGenericTypes
468
+ {
469
+ get
470
+ {
471
+ if ( s_knownSerializableGenericTypeInfos == null )
472
+ {
473
+ lock ( s_knownSerializableGenericTypeInfosLock )
474
+ {
475
+ if ( s_knownSerializableGenericTypeInfos == null )
476
+ {
477
+ s_knownSerializableGenericTypeInfos = new Dictionary < string , SerializableInfo > ( ) ;
478
+ InitializeKnownSerializableTypesFromStringList ( s_knownSerializableGenericTypeNames , s_knownSerializableGenericTypeInfos ) ;
479
+ }
480
+ }
481
+ }
482
+ return s_knownSerializableGenericTypeInfos ;
483
+ }
484
+ }
485
+
486
+ private static object s_knownSerializableNonGenericTypeInfosLock = new object ( ) ;
487
+ private static volatile IDictionary < string , SerializableInfo > s_knownSerializableNonGenericTypeInfos ;
488
+ private static IDictionary < string , SerializableInfo > KnownSerializableNonGenericTypes
489
+ {
490
+ get
491
+ {
492
+ if ( s_knownSerializableNonGenericTypeInfos == null )
493
+ {
494
+ lock ( s_knownSerializableNonGenericTypeInfosLock )
495
+ {
496
+ if ( s_knownSerializableNonGenericTypeInfos == null )
497
+ {
498
+ s_knownSerializableNonGenericTypeInfos = new Dictionary < string , SerializableInfo > ( ) ;
499
+ InitializeKnownSerializableTypesFromStringList ( s_knownSerializableNonGenericTypeNames , s_knownSerializableNonGenericTypeInfos ) ;
500
+ }
501
+ }
502
+ }
503
+ return s_knownSerializableNonGenericTypeInfos ;
504
+ }
505
+ }
506
+
507
+ private static void InitializeKnownSerializableTypesFromStringList ( string [ ] listOfTypes , IDictionary < string , SerializableInfo > dict )
508
+ {
509
+ Debug . Assert ( listOfTypes != null ) ;
510
+ Debug . Assert ( dict != null ) ;
511
+ foreach ( string str in listOfTypes )
512
+ {
513
+ SerializableInfo si = SerializableInfo . Parse ( str ) ;
514
+ dict . Add ( si . TypeName , si ) ;
515
+ }
516
+ }
517
+
462
518
internal static bool IsKnownSerializableType ( Type type )
463
519
{
464
520
// Applies to known types that DCS understands how to serialize/deserialize
465
521
//
466
522
467
- // Ajdust for generic type
468
- if ( type . GetTypeInfo ( ) . IsGenericType && ! type . GetTypeInfo ( ) . IsGenericTypeDefinition )
523
+ if ( type . GetTypeInfo ( ) . IsGenericType )
469
524
{
470
- type = type . GetGenericTypeDefinition ( ) ;
525
+ if ( ! type . GetTypeInfo ( ) . IsGenericTypeDefinition )
526
+ {
527
+ type = type . GetTypeInfo ( ) . GetGenericTypeDefinition ( ) ;
528
+ }
529
+ return Enumerable . Contains ( KnownSerializableGenericTypes . Keys , type . FullName ) ;
471
530
}
472
-
473
- // Check for known types
474
- if ( Enumerable . Contains ( s_knownSerializableTypeNames , type . FullName ) )
531
+ else if ( Enumerable . Contains ( KnownSerializableNonGenericTypes . Keys , type . FullName ) )
475
532
{
476
533
return true ;
477
534
}
478
- //Enable ClassDataContract to give support to Exceptions.
479
- if ( Globals . TypeOfException . IsAssignableFrom ( type ) )
535
+ else if ( Globals . TypeOfException . IsAssignableFrom ( type ) )
536
+ {
537
+ //Enable ClassDataContract to give support to Exceptions.
480
538
return true ;
539
+ }
540
+
541
+ return false ;
542
+ }
481
543
544
+ internal static bool IsNonSerializedMember ( Type type , string memberName )
545
+ {
546
+ if ( type . GetTypeInfo ( ) . IsGenericType )
547
+ {
548
+ Type genericTypeDef = type . GetTypeInfo ( ) . IsGenericTypeDefinition ? type : type . GetTypeInfo ( ) . GetGenericTypeDefinition ( ) ;
549
+ if ( KnownSerializableGenericTypes . Keys . Contains ( genericTypeDef . FullName ) )
550
+ {
551
+ return Enumerable . Contains ( KnownSerializableGenericTypes [ genericTypeDef . FullName ] . NonSerializedMembers , memberName ) ;
552
+ }
553
+ }
554
+ else if ( KnownSerializableNonGenericTypes . Keys . Contains ( type . FullName ) )
555
+ {
556
+ return Enumerable . Contains ( KnownSerializableNonGenericTypes [ type . FullName ] . NonSerializedMembers , memberName ) ;
557
+ }
482
558
return false ;
483
559
}
484
560
@@ -1073,8 +1149,7 @@ private void ImportDataMembers()
1073
1149
1074
1150
private static bool CanSerializeMember ( FieldInfo field )
1075
1151
{
1076
- return field != null &&
1077
- field . FieldType != Globals . TypeOfObject ; // Don't really know how to serialize plain System.Object instance;
1152
+ return field != null && ! ClassDataContract . IsNonSerializedMember ( field . DeclaringType , field . Name ) ;
1078
1153
}
1079
1154
1080
1155
private bool SetIfGetOnlyCollection ( DataMember memberContract )
0 commit comments