Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
CollectionDataContract.cs
Go to the documentation of this file.
4using System.Linq;
8using System.Xml;
9
11
12internal sealed class CollectionDataContract : DataContract
13{
15 {
17
19
20 private static Type[] s_knownInterfaces;
21
22 private Type _itemType;
23
24 private bool _isItemTypeNullable;
25
27
29
30 private readonly MethodInfo _addMethod;
31
32 private readonly ConstructorInfo _constructor;
33
34 private readonly string _deserializationExceptionMessage;
35
37
39
41
43
44 private string _itemName;
45
47
49
50 private string _keyName;
51
52 private string _valueName;
53
55
57
58 private XmlFormatCollectionReaderDelegate _xmlFormatReaderDelegate;
59
60 private XmlFormatGetOnlyCollectionReaderDelegate _xmlFormatGetOnlyCollectionReaderDelegate;
61
62 private XmlFormatCollectionWriterDelegate _xmlFormatWriterDelegate;
63
65
66 private IncrementCollectionCountDelegate _incrementCollectionCountDelegate;
67
69
70 private CreateGenericDictionaryEnumeratorDelegate _createGenericDictionaryEnumeratorDelegate;
71
73
95
97
98 internal Type ItemType
99 {
100 get
101 {
102 return _itemType;
103 }
104 set
105 {
107 }
108 }
109
111 {
112 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
113 get
114 {
115 if (_itemContract == null)
116 {
117 if (IsDictionary)
118 {
119 if (string.CompareOrdinal(KeyName, ValueName) == 0)
120 {
122 }
123 _itemContract = ClassDataContract.CreateClassDataContractForKeyValue(ItemType, base.Namespace, new string[2] { KeyName, ValueName });
125 }
126 else
127 {
129 if (_itemContract == null)
130 {
132 }
133 }
134 }
135 return _itemContract;
136 }
137 set
138 {
140 }
141 }
142
144 {
145 get
146 {
147 return _sharedTypeContract;
148 }
149 set
150 {
152 }
153 }
154
155 internal string ItemName
156 {
157 get
158 {
159 return _itemName;
160 }
161 set
162 {
164 }
165 }
166
168 {
169 get
170 {
172 }
173 set
174 {
176 }
177 }
178
180
181 internal string KeyName
182 {
183 get
184 {
185 return _keyName;
186 }
187 set
188 {
189 _keyName = value;
190 }
191 }
192
193 internal string ValueName
194 {
195 get
196 {
197 return _valueName;
198 }
199 set
200 {
202 }
203 }
204
205 internal bool IsDictionary => KeyName != null;
206
208
210 {
211 get
212 {
214 }
215 set
216 {
218 }
219 }
220
221 internal bool IsItemTypeNullable
222 {
223 get
224 {
225 return _isItemTypeNullable;
226 }
227 set
228 {
230 }
231 }
232
234
236
238
240 {
241 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
242 get
243 {
244 if (!_isKnownTypeAttributeChecked && base.UnderlyingType != null)
245 {
246 lock (this)
247 {
249 {
253 }
254 }
255 }
256 return _knownDataContracts;
257 }
258 set
259 {
261 }
262 }
263
265
267
268 internal XmlFormatCollectionWriterDelegate XmlFormatWriterDelegate
269 {
270 get
271 {
273 }
274 set
275 {
277 }
278 }
279
280 internal XmlFormatCollectionReaderDelegate XmlFormatReaderDelegate
281 {
282 get
283 {
285 }
286 set
287 {
289 }
290 }
291
292 internal XmlFormatGetOnlyCollectionReaderDelegate XmlFormatGetOnlyCollectionReaderDelegate
293 {
294 get
295 {
297 }
298 set
299 {
301 }
302 }
303
305 {
306 get
307 {
309 {
310 s_buildIncrementCollectionCountDelegateMethod = typeof(CollectionDataContractCriticalHelper).GetMethod("BuildIncrementCollectionCountDelegate", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
311 }
313 }
314 }
315
317 {
318 get
319 {
321 {
322 s_buildCreateGenericDictionaryEnumerator = typeof(CollectionDataContractCriticalHelper).GetMethod("BuildCreateGenericDictionaryEnumerator", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
323 }
325 }
326 }
327
328 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
330 {
331 _kind = kind;
332 if (itemType != null)
333 {
336 bool flag = kind == CollectionKind.Dictionary || kind == CollectionKind.GenericDictionary;
337 string text = null;
338 string text2 = null;
339 string text3 = null;
340 if (collectionContractAttribute != null)
341 {
342 if (collectionContractAttribute.IsItemNameSetExplicitly)
343 {
344 if (collectionContractAttribute.ItemName == null || collectionContractAttribute.ItemName.Length == 0)
345 {
347 }
350 }
351 if (collectionContractAttribute.IsKeyNameSetExplicitly)
352 {
353 if (collectionContractAttribute.KeyName == null || collectionContractAttribute.KeyName.Length == 0)
354 {
356 }
357 if (!flag)
358 {
360 }
362 }
363 if (collectionContractAttribute.IsValueNameSetExplicitly)
364 {
365 if (collectionContractAttribute.ValueName == null || collectionContractAttribute.ValueName.Length == 0)
366 {
368 }
369 if (!flag)
370 {
372 }
374 }
375 }
376 XmlDictionary xmlDictionary = (flag ? new XmlDictionary(5) : new XmlDictionary(3));
377 base.Name = xmlDictionary.Add(base.StableName.Name);
378 base.Namespace = xmlDictionary.Add(base.StableName.Namespace);
381 if (flag)
382 {
383 _keyName = text2 ?? "Key";
384 _valueName = text3 ?? "Value";
385 }
386 }
387 if (collectionContractAttribute != null)
388 {
390 }
391 }
392
393 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
408
409 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
426
427 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
429 : base(type)
430 {
431 if (getEnumeratorMethod == null)
432 {
434 }
435 if (addMethod == null && !type.IsInterface)
436 {
438 }
439 if (itemType == null)
440 {
442 }
448 }
449
450 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
456
457 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
464
468
470 {
472 {
473 switch (Kind)
474 {
476 case CollectionKind.List:
477 case CollectionKind.Collection:
479 {
481 };
482 break;
483 case CollectionKind.GenericList:
484 case CollectionKind.GenericCollection:
485 {
488 break;
489 }
490 case CollectionKind.GenericDictionary:
491 {
492 MethodInfo methodInfo = GetBuildIncrementCollectionCountGenericDelegate(typeof(KeyValuePair<, >).MakeGenericType(ItemType.GetGenericArguments()));
494 break;
495 }
496 default:
498 break;
499 }
500 }
502 [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2060:MakeGenericMethod", Justification = "The call to MakeGenericMethod is safe due to the fact that CollectionDataContractCriticalHelper.BuildIncrementCollectionCountDelegate<T> is not annotated.")]
504 {
505 return BuildIncrementCollectionCountDelegateMethod.MakeGenericMethod(type);
506 }
507 }
508
509 [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2060:MakeGenericMethod", Justification = "The call to MakeGenericMethod is safe due to the fact that CollectionDataContractCriticalHelper.BuildIncrementCollectionCountDelegate<T> is not annotated.")]
514
515 private static IncrementCollectionCountDelegate BuildIncrementCollectionCountDelegate<T>()
516 {
518 {
519 context.IncrementCollectionCountGeneric(xmlwriter, (ICollection<T>)obj);
520 };
521 }
522
524 {
526 if (Kind == CollectionKind.GenericDictionary)
527 {
529 {
530 Type[] genericArguments = ItemType.GetGenericArguments();
533 }
535 }
536 else if (Kind == CollectionKind.Dictionary)
537 {
538 enumerator = new DictionaryEnumerator(((IDictionary)obj).GetEnumerator());
539 }
540 return enumerator;
541 [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2060:MakeGenericMethod", Justification = "The call to MakeGenericMethod is safe due to the fact that CollectionDataContractCriticalHelper.BuildCreateGenericDictionaryEnumerator<K,V> is not annotated.")]
543 {
545 }
546 }
547
548 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
550 {
551 Type type = null;
552 if (Kind != CollectionKind.GenericDictionary)
553 {
555 }
556 else
557 {
558 Type[] genericArguments = ItemType.GetGenericArguments();
560 }
561 MethodInfo methodInfo = type.GetMethod("get_Current", BindingFlags.Instance | BindingFlags.Public, Type.EmptyTypes);
562 if (methodInfo == null)
563 {
564 if (type.IsInterface)
565 {
567 }
568 else
569 {
571 if (Kind == CollectionKind.GenericDictionary || Kind == CollectionKind.GenericCollection || Kind == CollectionKind.GenericEnumerable)
572 {
573 Type[] interfaces = type.GetInterfaces();
574 Type[] array = interfaces;
575 foreach (Type type2 in array)
576 {
577 if (type2.IsGenericType && type2.GetGenericTypeDefinition() == Globals.TypeOfIEnumeratorGeneric && type2.GetGenericArguments()[0] == ItemType)
578 {
580 break;
581 }
582 }
583 }
585 }
586 }
587 return methodInfo.ReturnType;
588 }
589
590 private static CreateGenericDictionaryEnumeratorDelegate BuildCreateGenericDictionaryEnumerator<K, V>()
591 {
593 }
594 }
595
596 internal sealed class DictionaryEnumerator : IEnumerator<KeyValue<object, object>>, IEnumerator, IDisposable
597 {
599
601
603
608
609 public void Dispose()
610 {
611 GC.SuppressFinalize(this);
612 }
613
614 public bool MoveNext()
615 {
616 return _enumerator.MoveNext();
617 }
618
619 public void Reset()
620 {
622 }
623 }
624
625 internal sealed class GenericDictionaryEnumerator<K, V> : IEnumerator<KeyValue<K, V>>, IEnumerator, IDisposable
626 {
628
630 {
631 get
632 {
634 return new KeyValue<K, V>(current.Key, current.Value);
635 }
636 }
637
639
644
645 public void Dispose()
646 {
647 GC.SuppressFinalize(this);
648 }
649
650 public bool MoveNext()
651 {
652 return _enumerator.MoveNext();
653 }
654
655 public void Reset()
656 {
657 _enumerator.Reset();
658 }
659 }
660
662
664
666
668
670
672
673 public Type ItemType
674 {
675 get
676 {
677 return _helper.ItemType;
678 }
679 set
680 {
682 }
683 }
684
686 {
687 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
688 get
689 {
691 }
692 set
693 {
696 }
697 }
698
700
701 public string ItemName
702 {
703 get
704 {
705 return _helper.ItemName;
706 }
707 set
708 {
710 }
711 }
712
714 {
715 get
716 {
717 return _collectionItemName;
718 }
719 set
720 {
722 }
723 }
724
725 public string KeyName
726 {
727 get
728 {
729 return _helper.KeyName;
730 }
731 set
732 {
734 }
735 }
736
737 public string ValueName
738 {
739 get
740 {
741 return _helper.ValueName;
742 }
743 set
744 {
746 }
747 }
748
749 internal bool IsDictionary => KeyName != null;
750
752 {
753 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
754 get
755 {
756 if (_childElementNamespace == null)
757 {
758 lock (this)
759 {
760 if (_childElementNamespace == null)
761 {
763 {
767 }
769 }
770 }
771 }
773 }
774 }
775
776 internal bool IsItemTypeNullable
777 {
778 get
779 {
781 }
782 set
783 {
785 }
786 }
787
789 {
790 get
791 {
793 }
794 set
795 {
797 }
798 }
799
801
803
805
807 {
808 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
809 get
810 {
812 }
813 set
814 {
816 }
817 }
818
820
822
824
825 internal XmlFormatCollectionWriterDelegate XmlFormatWriterDelegate
826 {
827 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
828 get
829 {
831 {
832 lock (this)
833 {
835 {
836 XmlFormatCollectionWriterDelegate xmlFormatWriterDelegate = CreateXmlFormatWriterDelegate();
839 }
840 }
841 }
843 }
844 set
845 {
846 }
847 }
848
849 internal XmlFormatCollectionReaderDelegate XmlFormatReaderDelegate
850 {
851 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
852 get
853 {
855 {
856 lock (this)
857 {
859 {
861 {
863 }
864 XmlFormatCollectionReaderDelegate xmlFormatReaderDelegate = CreateXmlFormatReaderDelegate();
867 }
868 }
869 }
871 }
872 set
873 {
874 }
875 }
876
877 internal XmlFormatGetOnlyCollectionReaderDelegate XmlFormatGetOnlyCollectionReaderDelegate
878 {
879 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
880 get
881 {
883 {
884 lock (this)
885 {
887 {
888 if (base.UnderlyingType.IsInterface && (Kind == CollectionKind.Enumerable || Kind == CollectionKind.Collection || Kind == CollectionKind.GenericEnumerable))
889 {
891 }
893 {
895 }
896 if (Kind != CollectionKind.Array && AddMethod == null)
897 {
899 }
903 }
904 }
905 }
907 }
908 set
909 {
910 }
911 }
912
913 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
919
920 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
926
927 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
933
934 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
940
941 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
947
948 [MemberNotNull("_helper")]
949 [MemberNotNull("_collectionItemName")]
950 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
961
962 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
963 private XmlFormatCollectionWriterDelegate CreateXmlFormatWriterDelegate()
964 {
966 }
967
968 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
969 private XmlFormatCollectionReaderDelegate CreateXmlFormatReaderDelegate()
970 {
972 }
973
974 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
975 private XmlFormatGetOnlyCollectionReaderDelegate CreateXmlFormatGetOnlyCollectionReaderDelegate()
976 {
978 }
979
984
989
990 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
992 {
994 }
995
996 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
998 {
1000 {
1001 return this;
1002 }
1003 if (type.IsDefined(Globals.TypeOfDataContractAttribute, inherit: false))
1004 {
1005 return new ClassDataContract(type);
1006 }
1007 return null;
1008 }
1009
1010 internal static bool IsCollectionInterface(Type type)
1011 {
1012 if (type.IsGenericType)
1013 {
1014 type = type.GetGenericTypeDefinition();
1015 }
1016 return ((ICollection<Type>)KnownInterfaces).Contains(type);
1017 }
1018
1019 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
1020 internal static bool IsCollection(Type type)
1021 {
1022 Type itemType;
1023 return IsCollection(type, out itemType);
1024 }
1025
1026 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
1027 internal static bool IsCollection(Type type, [NotNullWhen(true)] out Type itemType)
1028 {
1030 }
1031
1032 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
1033 internal static bool IsCollection(Type type, bool constructorRequired)
1034 {
1035 Type itemType;
1037 }
1038
1039 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
1041 {
1042 if (type.IsArray && DataContract.GetBuiltInDataContract(type) == null)
1043 {
1044 itemType = type.GetElementType();
1045 return true;
1046 }
1049 }
1050
1051 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
1057
1058 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
1060 {
1061 if (type.IsArray)
1062 {
1064 return true;
1065 }
1066 Type itemType;
1068 }
1069
1070 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
1072 {
1074 if (dataContract == null)
1075 {
1076 if (type.IsArray)
1077 {
1079 return true;
1080 }
1081 Type itemType;
1083 }
1085 {
1086 return true;
1087 }
1088 dataContract = null;
1089 return false;
1090 }
1091
1092 [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:GetMethod", Justification = "The DynamicallyAccessedMembers declarations will ensure the interface methods will be preserved.")]
1094 {
1095 return (from it in type.GetInterfaces()
1096 where it.Equals(interfaceType)
1097 select it).FirstOrDefault()?.GetMethod(name);
1098 }
1099
1100 private static bool IsArraySegment(Type t)
1101 {
1102 if (t.IsGenericType)
1103 {
1105 }
1106 return false;
1107 }
1108
1109 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
1111 {
1112 dataContract = null;
1115 {
1117 }
1119 bool flag = false;
1120 string deserializationExceptionMessage = null;
1121 Type baseType = type.BaseType;
1123 if (type.IsDefined(Globals.TypeOfDataContractAttribute, inherit: false))
1124 {
1126 }
1127 if (Globals.TypeOfIXmlSerializable.IsAssignableFrom(type) || IsArraySegment(type))
1128 {
1129 return false;
1130 }
1131 if (!Globals.TypeOfIEnumerable.IsAssignableFrom(type))
1132 {
1134 }
1137 if (type.IsInterface)
1138 {
1139 Type type2 = (type.IsGenericType ? type.GetGenericTypeDefinition() : type);
1141 for (int i = 0; i < knownInterfaces.Length; i++)
1142 {
1143 if (!(knownInterfaces[i] == type2))
1144 {
1145 continue;
1146 }
1147 addMethod = null;
1148 if (type.IsGenericType)
1149 {
1150 Type[] genericArguments = type.GetGenericArguments();
1152 {
1154 addMethod = type.GetMethod("Add");
1155 method = Globals.TypeOfIEnumerableGeneric.MakeGenericType(Globals.TypeOfKeyValuePair.MakeGenericType(genericArguments)).GetMethod("GetEnumerator");
1156 }
1157 else
1158 {
1161 {
1162 addMethod = Globals.TypeOfICollectionGeneric.MakeGenericType(itemType).GetMethod("Add");
1163 }
1164 method = Globals.TypeOfIEnumerableGeneric.MakeGenericType(itemType).GetMethod("GetEnumerator");
1165 }
1166 }
1167 else
1168 {
1170 {
1172 addMethod = type.GetMethod("Add");
1173 }
1174 else
1175 {
1177 if (type2 == Globals.TypeOfIList)
1178 {
1179 addMethod = type.GetMethod("Add");
1180 }
1181 }
1182 method = typeof(IEnumerable).GetMethod("GetEnumerator");
1183 }
1184 if (tryCreate)
1185 {
1187 }
1188 return true;
1189 }
1190 }
1192 if (!type.IsValueType)
1193 {
1194 constructorInfo = type.GetConstructor(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, Type.EmptyTypes);
1195 if (constructorInfo == null && constructorRequired)
1196 {
1197 if (type.IsSerializable)
1198 {
1200 }
1201 flag = true;
1203 }
1204 }
1205 Type type3 = null;
1207 bool flag2 = false;
1208 Type[] interfaces = type.GetInterfaces();
1209 Type[] array = interfaces;
1210 foreach (Type type4 in array)
1211 {
1212 Type type5 = (type4.IsGenericType ? type4.GetGenericTypeDefinition() : type4);
1214 for (int k = 0; k < knownInterfaces2.Length; k++)
1215 {
1216 if (knownInterfaces2[k] == type5)
1217 {
1220 {
1222 type3 = type4;
1223 flag2 = false;
1224 }
1226 {
1227 flag2 = true;
1228 }
1229 break;
1230 }
1231 }
1232 }
1233 switch (collectionKind)
1234 {
1235 case CollectionKind.None:
1237 case CollectionKind.GenericEnumerable:
1238 case CollectionKind.Collection:
1239 case CollectionKind.Enumerable:
1240 if (flag2)
1241 {
1243 }
1244 itemType = (type3.IsGenericType ? type3.GetGenericArguments()[0] : Globals.TypeOfObject);
1246 if (addMethod == null)
1247 {
1248 if (type.IsSerializable)
1249 {
1251 }
1252 flag = true;
1254 }
1255 if (tryCreate)
1256 {
1258 }
1259 break;
1260 default:
1261 {
1262 if (flag2)
1263 {
1265 }
1266 Type[] array2 = null;
1267 switch (collectionKind)
1268 {
1269 case CollectionKind.GenericDictionary:
1270 {
1271 array2 = type3.GetGenericArguments();
1272 bool flag3 = type3.IsGenericTypeDefinition || (array2[0].IsGenericParameter && array2[1].IsGenericParameter);
1274 break;
1275 }
1277 array2 = new Type[2]
1278 {
1281 };
1282 itemType = Globals.TypeOfKeyValue.MakeGenericType(array2);
1283 break;
1284 case CollectionKind.GenericList:
1285 case CollectionKind.GenericCollection:
1286 array2 = type3.GetGenericArguments();
1287 itemType = array2[0];
1288 break;
1289 case CollectionKind.List:
1291 array2 = new Type[1] { itemType };
1292 break;
1293 }
1294 if (tryCreate)
1295 {
1298 }
1299 break;
1300 }
1301 }
1302 return true;
1303 }
1304
1305 internal static bool IsCollectionDataContract(Type type)
1306 {
1308 }
1309
1310 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
1331
1336
1337 private static string GetInvalidCollectionMessage(string message, string nestedMessage, string param)
1338 {
1339 if (param != null)
1340 {
1341 return System.SR.Format(message, nestedMessage, param);
1342 }
1343 return System.SR.Format(message, nestedMessage);
1344 }
1345
1346 [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:GetMethod", Justification = "The DynamicallyAccessedMembers declarations will ensure the interface methods will be preserved.")]
1348 {
1349 Type type2 = (from it in type.GetInterfaces()
1350 where it.Equals(interfaceType)
1351 select it).FirstOrDefault();
1352 if (type2 != null)
1353 {
1354 addMethod = type2.GetMethod("Add") ?? addMethod;
1355 getEnumeratorMethod = type2.GetMethod("GetEnumerator") ?? getEnumeratorMethod;
1356 }
1357 }
1358
1359 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
1361 {
1362 addMethod = (getEnumeratorMethod = null);
1364 {
1365 addMethod = type.GetMethod("Add", BindingFlags.Instance | BindingFlags.Public, addMethodTypeArray);
1366 if (addMethod == null || addMethod.GetParameters()[0].ParameterType != addMethodTypeArray[0])
1367 {
1369 if (addMethod == null)
1370 {
1371 Type[] interfaces = interfaceType.GetInterfaces();
1372 Array.Sort(interfaces, (Type x, Type y) => string.Compare(x.FullName, y.FullName));
1373 Type[] array = interfaces;
1374 foreach (Type type2 in array)
1375 {
1377 {
1379 if (addMethod == null)
1380 {
1381 break;
1382 }
1383 }
1384 }
1385 }
1386 }
1387 }
1388 else
1389 {
1390 addMethod = type.GetMethod("Add", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, addMethodTypeArray);
1391 }
1392 if (!(getEnumeratorMethod == null))
1393 {
1394 return;
1395 }
1396 getEnumeratorMethod = type.GetMethod("GetEnumerator", BindingFlags.Instance | BindingFlags.Public, Type.EmptyTypes);
1397 if (getEnumeratorMethod == null || !Globals.TypeOfIEnumerator.IsAssignableFrom(getEnumeratorMethod.ReturnType))
1398 {
1399 Type type3 = (from t in interfaceType.GetInterfaces()
1400 where t.FullName.StartsWith("System.Collections.Generic.IEnumerable")
1401 select t).FirstOrDefault();
1402 if (type3 == null)
1403 {
1405 }
1407 }
1408 }
1409
1414
1415 private static bool IsKnownInterface(Type type)
1416 {
1417 Type type2 = (type.IsGenericType ? type.GetGenericTypeDefinition() : type);
1419 foreach (Type type3 in knownInterfaces)
1420 {
1421 if (type2 == type3)
1422 {
1423 return true;
1424 }
1425 }
1426 return false;
1427 }
1428
1437
1438 internal override DataContract GetValidContract()
1439 {
1441 {
1443 }
1444 return this;
1445 }
1446
1455
1456 internal override bool IsValidContract(SerializationMode mode)
1457 {
1459 }
1460
1462 {
1463 if (!DataContract.IsTypeVisible(base.UnderlyingType))
1464 {
1465 if (securityException != null)
1466 {
1468 }
1469 return true;
1470 }
1471 if (ItemType != null && !DataContract.IsTypeVisible(ItemType))
1472 {
1473 if (securityException != null)
1474 {
1476 }
1477 return true;
1478 }
1480 {
1481 if (securityException != null)
1482 {
1484 }
1485 return true;
1486 }
1488 {
1489 if (securityException != null)
1490 {
1492 }
1493 return true;
1494 }
1495 return false;
1496 }
1497
1499 {
1500 if (!DataContract.IsTypeVisible(base.UnderlyingType))
1501 {
1502 if (securityException != null)
1503 {
1505 }
1506 return true;
1507 }
1508 if (ItemType != null && !DataContract.IsTypeVisible(ItemType))
1509 {
1510 if (securityException != null)
1511 {
1513 }
1514 return true;
1515 }
1516 return false;
1517 }
1518
1519 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
1521 {
1523 XmlFormatWriterDelegate(xmlWriter, obj, context, this);
1524 }
1525
1526 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
1528 {
1529 xmlReader.Read();
1530 object result = null;
1531 if (context.IsGetOnlyCollection)
1532 {
1535 }
1536 else
1537 {
1539 }
1540 xmlReader.ReadEndElement();
1541 return result;
1542 }
1543}
static void Sort(Array array)
Definition Array.cs:2329
void Add(TKey key, TValue value)
static void SuppressFinalize(object obj)
Definition GC.cs:202
Definition GC.cs:8
static XmlDictionaryString GetChildNamespaceToDeclare(DataContract dataContract, Type childType, XmlDictionary dictionary)
static ClassDataContract CreateClassDataContractForKeyValue(Type type, XmlDictionaryString ns, string[] memberNames)
void Init(CollectionKind kind, Type itemType, CollectionDataContractAttribute collectionContractAttribute)
static void DummyIncrementCollectionCount(XmlWriterDelegator xmlWriter, object obj, XmlObjectSerializerWriteContext context)
CollectionDataContractCriticalHelper(Type type, CollectionKind kind, Type itemType, MethodInfo getEnumeratorMethod, MethodInfo addMethod, ConstructorInfo constructor, bool isConstructorCheckRequired)
CollectionDataContractCriticalHelper([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors|DynamicallyAccessedMemberTypes.NonPublicConstructors|DynamicallyAccessedMemberTypes.PublicMethods|DynamicallyAccessedMemberTypes.NonPublicMethods|DynamicallyAccessedMemberTypes.PublicFields|DynamicallyAccessedMemberTypes.PublicProperties)] Type type)
void IncrementCollectionCount(XmlWriterDelegator xmlWriter, object obj, XmlObjectSerializerWriteContext context)
CollectionDataContractCriticalHelper([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors|DynamicallyAccessedMemberTypes.NonPublicConstructors|DynamicallyAccessedMemberTypes.PublicMethods|DynamicallyAccessedMemberTypes.NonPublicMethods|DynamicallyAccessedMemberTypes.PublicFields|DynamicallyAccessedMemberTypes.PublicProperties)] Type type, CollectionKind kind, Type itemType, MethodInfo getEnumeratorMethod, MethodInfo addMethod, ConstructorInfo constructor)
delegate void IncrementCollectionCountDelegate(XmlWriterDelegator xmlWriter, object obj, XmlObjectSerializerWriteContext context)
CollectionDataContractCriticalHelper([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors|DynamicallyAccessedMemberTypes.NonPublicConstructors|DynamicallyAccessedMemberTypes.PublicMethods|DynamicallyAccessedMemberTypes.NonPublicMethods|DynamicallyAccessedMemberTypes.PublicFields|DynamicallyAccessedMemberTypes.PublicProperties)] Type type, CollectionKind kind, Type itemType, MethodInfo getEnumeratorMethod, string deserializationExceptionMessage)
CollectionDataContractCriticalHelper([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors|DynamicallyAccessedMemberTypes.NonPublicConstructors|DynamicallyAccessedMemberTypes.PublicMethods|DynamicallyAccessedMemberTypes.NonPublicMethods|DynamicallyAccessedMemberTypes.PublicFields|DynamicallyAccessedMemberTypes.PublicProperties)] Type type, string invalidCollectionInSharedContractMessage)
static bool IsCollection(Type type, [NotNullWhen(true)] out Type itemType)
static bool IsCollection(Type type, bool constructorRequired)
static void GetCollectionMethods([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type type, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] Type interfaceType, Type[] addMethodTypeArray, bool addMethodOnInterface, out MethodInfo getEnumeratorMethod, out MethodInfo addMethod)
CollectionDataContract(Type type, string invalidCollectionInSharedContractMessage)
void IncrementCollectionCount(XmlWriterDelegator xmlWriter, object obj, XmlObjectSerializerWriteContext context)
static bool IsCollectionHelper(Type type, [NotNullWhen(true)] out Type itemType, bool constructorRequired)
bool RequiresMemberAccessForRead(SecurityException securityException)
static void FindCollectionMethodsOnInterface([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type type, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] Type interfaceType, ref MethodInfo addMethod, ref MethodInfo getEnumeratorMethod)
static MethodInfo GetIEnumerableGetEnumeratorMethod([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type type, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] Type ienumerableInterface)
XmlFormatCollectionReaderDelegate CreateXmlFormatReaderDelegate()
CollectionDataContract(Type type, CollectionKind kind, Type itemType, MethodInfo getEnumeratorMethod, string deserializationExceptionMessage)
XmlFormatGetOnlyCollectionReaderDelegate CreateXmlFormatGetOnlyCollectionReaderDelegate()
static MethodInfo GetTargetMethodWithName(string name, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type type, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] Type interfaceType)
static bool CreateGetOnlyCollectionDataContract(Type type, [NotNullWhen(true)] out DataContract dataContract)
static bool TryCreate(Type type, [NotNullWhen(true)] out DataContract dataContract)
static bool IsCollectionOrTryCreate(Type type, bool tryCreate, out DataContract dataContract, out Type itemType, bool constructorRequired)
override Dictionary< XmlQualifiedName, DataContract > KnownDataContracts
static string GetInvalidCollectionMessage(string message, string nestedMessage, string param)
XmlFormatCollectionWriterDelegate CreateXmlFormatWriterDelegate()
override void WriteXmlValue(XmlWriterDelegator xmlWriter, object obj, XmlObjectSerializerWriteContext context)
bool RequiresMemberAccessForWrite(SecurityException securityException)
XmlFormatGetOnlyCollectionReaderDelegate XmlFormatGetOnlyCollectionReaderDelegate
void InitCollectionDataContract(DataContract sharedTypeContract)
CollectionDataContract(Type type, CollectionKind kind, Type itemType, MethodInfo getEnumeratorMethod, MethodInfo addMethod, ConstructorInfo constructor)
CollectionDataContract(Type type, CollectionKind kind, Type itemType, MethodInfo getEnumeratorMethod, MethodInfo addMethod, ConstructorInfo constructor, bool isConstructorCheckRequired)
static void GetReadOnlyCollectionExceptionMessages(Type type, string message, string param, out string deserializationExceptionMessage)
override DataContract GetValidContract(SerializationMode mode)
static bool TryCreateGetOnlyCollectionDataContract(Type type, [NotNullWhen(true)] out DataContract dataContract)
static bool HandleIfInvalidCollection(Type type, bool tryCreate, bool hasCollectionDataContract, bool createContractWithException, string message, string param, ref DataContract dataContract)
override object ReadXmlValue(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context)
static string EncodeLocalName(string localName)
static Dictionary< XmlQualifiedName, DataContract > ImportKnownTypeAttributes(Type type)
static DataContract GetBuiltInDataContract(Type type)
static XmlQualifiedName GetCollectionStableName(Type type, Type itemType, out CollectionDataContractAttribute collectionContractAttribute)
static string GetClrTypeFullName(Type type)
static bool ConstructorRequiresMemberAccess(ConstructorInfo ctor)
static DataContract GetDataContract(Type type)
static bool MethodRequiresMemberAccess(MethodInfo method)
virtual XmlDictionaryString Namespace
static XmlQualifiedName GetStableName(Type type)
static void ThrowInvalidDataContractException(string message, Type type)
static DataContract GetDataContractFromGeneratedAssembly(Type type)
static Type TypeOfCollectionDataContractAttribute
Definition Globals.cs:729
XmlFormatCollectionReaderDelegate GenerateCollectionReader(CollectionDataContract collectionContract)
XmlFormatGetOnlyCollectionReaderDelegate GenerateGetOnlyCollectionReader(CollectionDataContract collectionContract)
XmlFormatCollectionWriterDelegate GenerateCollectionWriter(CollectionDataContract collectionContract)
void IncrementCollectionCount(XmlWriterDelegator xmlWriter, ICollection collection)
static string InvalidCollectionDataContract
Definition SR.cs:134
static string CollectionTypeDoesNotHaveDefaultCtor
Definition SR.cs:48
static string CollectionTypeCannotHaveDataContract
Definition SR.cs:44
static string InvalidCollectionContractKeyNoDictionary
Definition SR.cs:124
static string GetOnlyCollectionMustHaveAddMethod
Definition SR.cs:532
static string CollectionTypeHasMultipleDefinitionsOfInterface
Definition SR.cs:50
static string DupKeyValueName
Definition SR.cs:74
static string CollectionTypeDoesNotHaveAddMethod
Definition SR.cs:46
static string CollectionTypeCannotBeBuiltIn
Definition SR.cs:42
static string InvalidCollectionContractValueName
Definition SR.cs:130
static string PartialTrustCollectionContractAddMethodNotPublic
Definition SR.cs:228
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string InvalidCollectionContractKeyName
Definition SR.cs:122
static string CollectionMustHaveItemType
Definition SR.cs:40
static string InvalidCollectionType
Definition SR.cs:136
static string InvalidCollectionContractItemName
Definition SR.cs:120
static string ReadOnlyCollectionDeserialization
Definition SR.cs:712
static string SupportForMultidimensionalArraysNotPresent
Definition SR.cs:274
static string CollectionMustHaveAddMethod
Definition SR.cs:36
static string PartialTrustCollectionContractNoPublicConstructor
Definition SR.cs:230
static string CollectionMustHaveGetEnumeratorMethod
Definition SR.cs:38
static string PartialTrustCollectionContractTypeNotPublic
Definition SR.cs:232
static string CollectionTypeIsNotIEnumerable
Definition SR.cs:52
static string InvalidCollectionContractValueNoDictionary
Definition SR.cs:132
Definition SR.cs:7
virtual bool IsGenericType
Definition Type.cs:111
string? FullName
Definition Type.cs:47
Type[] GetInterfaces()
static readonly Type[] EmptyTypes
Definition Type.cs:19
virtual Type GetGenericTypeDefinition()
Definition Type.cs:495