Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ReflectionReader.cs
Go to the documentation of this file.
5using System.Xml;
6
8
9internal abstract class ReflectionReader
10{
12
14
16
17 private static readonly MethodInfo s_objectToKeyValuePairGetKey = typeof(ReflectionReader).GetMethod("ObjectToKeyValuePairGetKey", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
18
19 private static readonly MethodInfo s_objectToKeyValuePairGetValue = typeof(ReflectionReader).GetMethod("ObjectToKeyValuePairGetValue", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
20
21 private static readonly Type[] s_arrayConstructorParameters = new Type[1] { Globals.TypeOfInt };
22
23 private static readonly object[] s_arrayConstructorArguments = new object[1] { 32 };
24
26 {
27 get
28 {
30 {
31 s_getCollectionSetItemDelegateMethod = typeof(ReflectionReader).GetMethod("GetCollectionSetItemDelegate", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
32 }
34 }
35 }
36
37 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
60
61 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
75
76 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
81
82 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
102
103 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
105 {
106 if (collectionContract.Kind == CollectionKind.Dictionary || collectionContract.Kind == CollectionKind.GenericDictionary)
107 {
109 }
111 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
113 {
115 }
116 }
117
118 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
120 {
127 int num = 0;
128 while (true)
129 {
131 {
134 num++;
135 continue;
136 }
137 if (xmlReader.NodeType == XmlNodeType.EndElement)
138 {
139 break;
140 }
141 if (!xmlReader.IsStartElement())
142 {
143 throw XmlObjectSerializerReadContext.CreateUnexpectedStateException(XmlNodeType.Element, xmlReader);
144 }
145 context.SkipUnknownElement(xmlReader);
146 }
147 context.IncrementItemCount(num);
149 {
150 MethodInfo methodInfo2 = XmlFormatGeneratorStatics.TrimArraySizeMethod.MakeGenericMethod(itemType);
151 resultCollection = methodInfo2.Invoke(null, new object[2] { resultCollection, num });
152 }
153 return resultCollection;
154 }
155
156 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
158
159 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
161
163
165
167
168 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
173
175 {
176 int num = ((classContract.BaseContract != null) ? ReflectionGetMembers(classContract.BaseContract, members) : 0);
177 int num2 = num;
178 int num3 = 0;
179 while (num3 < classContract.Members.Count)
180 {
181 members[num2 + num3] = classContract.Members[num3];
182 num3++;
183 num++;
184 }
185 return num;
186 }
187
188 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
190 {
192 if (dataMember.IsGetOnlyCollection)
193 {
194 object collectionMember = ReflectionGetMemberValue(obj, dataMember);
196 ReflectionReadValue(xmlReader, context, dataMember, GetClassContractNamespace(classContract));
197 }
198 else
199 {
201 object memberValue = ReflectionReadValue(xmlReader, context, dataMember, classContract.StableName.Namespace);
202 MemberInfo memberInfo = dataMember.MemberInfo;
203 ReflectionSetMemberValue(ref obj, memberValue, dataMember);
204 }
205 }
206
207 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
209 {
210 object obj = null;
211 int num = 0;
212 while (type.IsGenericType && type.GetGenericTypeDefinition() == Globals.TypeOfNullable)
213 {
214 num++;
215 type = type.GetGenericArguments()[0];
216 }
218 if ((primitiveDataContract != null && primitiveDataContract.UnderlyingType != Globals.TypeOfObject) || num != 0 || type.IsValueType)
219 {
220 return ReadItemOfPrimitiveType(xmlReader, context, type, name, ns, primitiveDataContract, num);
221 }
222 return ReflectionInternalDeserialize(xmlReader, context, null, type, name, ns);
223 }
224
225 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
227 {
228 context.ReadAttributes(xmlReader);
230 bool isValueType = type.IsValueType;
231 if (text != null)
232 {
233 if (text.Length == 0)
234 {
235 text = context.GetObjectId();
236 if (!string.IsNullOrEmpty(text) && isValueType)
237 {
239 }
240 if (primitiveContract != null && primitiveContract.UnderlyingType != Globals.TypeOfObject)
241 {
242 return primitiveContract.ReadXmlValue(xmlReader, context);
243 }
244 return ReflectionInternalDeserialize(xmlReader, context, null, type, name, ns);
245 }
246 if (isValueType)
247 {
249 }
250 return context.GetExistingObject(text, type, name, ns);
251 }
252 if (isValueType && nullables == 0)
253 {
255 }
256 return null;
257 }
258
259 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
267
268 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
269 private CollectionReadItemDelegate GetReflectionReadValueDelegate(Type type)
270 {
271 int nullables = 0;
272 while (type.IsGenericType && type.GetGenericTypeDefinition() == Globals.TypeOfNullable)
273 {
274 nullables++;
275 type = type.GetGenericArguments()[0];
276 }
278 if ((primitiveContract != null && primitiveContract.UnderlyingType != Globals.TypeOfObject) || nullables != 0 || type.IsValueType)
279 {
281 }
282 return ReflectionInternalDeserialize;
283 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
285 {
286 return ReadItemOfPrimitiveType(xmlReader, context, itemType, itemName, itemNs, primitiveContract, nullables);
287 }
288 }
289
291 {
292 return dataMember.Getter(obj);
293 }
294
296 {
297 dataMember.Setter(ref obj, memberValue);
298 }
299
300 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
302 {
303 Type memberType = dataMember.MemberType;
304 string name = dataMember.Name;
305 return ReflectionReadValue(xmlReader, context, memberType, name, ns, dataMember.MemberPrimitiveContract);
306 }
307
308 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
310 {
311 return context.InternalDeserialize(xmlReader, DataContract.GetId(type.TypeHandle), type.TypeHandle, name, ns);
312 }
313
315 {
316 if (classContract.BaseContract != null)
317 {
318 InvokeOnDeserializing(context, classContract.BaseContract, obj);
319 }
320 if (classContract.OnDeserializing != null)
321 {
323 classContract.OnDeserializing.Invoke(obj, new object[1] { streamingContext });
324 }
325 }
326
328 {
329 if (classContract.BaseContract != null)
330 {
331 InvokeOnDeserialized(context, classContract.BaseContract, obj);
332 }
333 if (classContract.OnDeserialized != null)
334 {
336 classContract.OnDeserialized.Invoke(obj, new object[1] { streamingContext });
337 }
338 }
339
347
349 {
350 if (!classContract.CreateNewInstanceViaDefaultConstructor(out var obj))
351 {
352 Type underlyingType = classContract.UnderlyingType;
354 }
355 return obj;
356 }
357
359 {
360 Type type = obj.GetType();
362 {
364 }
366 {
368 }
369 else if (obj is IKeyValuePairAdapter)
370 {
371 obj = classContract.GetKeyValuePairMethodInfo.Invoke(obj, Array.Empty<object>());
372 }
373 return obj;
374 }
375
377 {
378 if (collectionContract.UnderlyingType.IsInterface)
379 {
381 if (kind - 3 <= CollectionKind.List)
382 {
383 return true;
384 }
385 }
386 return false;
387 }
388
390 {
391 if (collectionContract.Kind != CollectionKind.Array)
392 {
393 return IsArrayLikeInterface(collectionContract);
394 }
395 return true;
396 }
397
398 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
400 {
401 if (IsArrayLikeCollection(collectionContract))
402 {
403 Type type = collectionContract.ItemType.MakeArrayType();
404 ConstructorInfo constructor = type.GetConstructor(s_arrayConstructorParameters);
405 return constructor.Invoke(s_arrayConstructorArguments);
406 }
407 if (collectionContract.Kind == CollectionKind.GenericDictionary && collectionContract.UnderlyingType.IsInterface)
408 {
409 Type type2 = Globals.TypeOfDictionaryGeneric.MakeGenericType(collectionContract.ItemType.GetGenericArguments());
410 ConstructorInfo constructor2 = type2.GetConstructor(BindingFlags.Instance | BindingFlags.Public, Type.EmptyTypes);
411 return constructor2.Invoke(Array.Empty<object>());
412 }
413 if (collectionContract.UnderlyingType.IsValueType)
414 {
415 return Activator.CreateInstance(collectionContract.UnderlyingType);
416 }
417 if (collectionContract.UnderlyingType == Globals.TypeOfIDictionary)
418 {
419 return new Dictionary<object, object>();
420 }
422 return constructor3.Invoke(Array.Empty<object>());
423 }
424
425 private static object ObjectToKeyValuePairGetKey<K, V>(object o)
426 {
427 return ((KeyValue<K, V>)o).Key;
428 }
429
430 private static object ObjectToKeyValuePairGetValue<K, V>(object o)
431 {
432 return ((KeyValue<K, V>)o).Value;
433 }
434
436 {
438 {
439 int arraySize = ((Array)resultCollectionObject).Length;
440 return delegate(object resultCollection, object collectionItem, int index)
441 {
442 if (index == arraySize)
443 {
445 }
447 return resultCollection;
448 };
449 }
450 if (!isReadOnlyCollection && IsArrayLikeCollection(collectionContract))
451 {
452 return delegate(object resultCollection, object collectionItem, int index)
453 {
456 return resultCollection;
457 };
458 }
459 if (collectionContract.Kind == CollectionKind.GenericDictionary || collectionContract.Kind == CollectionKind.Dictionary)
460 {
461 Type keyType2 = collectionContract.ItemType.GenericTypeArguments[0];
462 Type valueType2 = collectionContract.ItemType.GenericTypeArguments[1];
463 Func<object, object> objectToKeyValuePairGetKey = MakeGenericMethod(s_objectToKeyValuePairGetKey, keyType2, valueType2).CreateDelegate<Func<object, object>>();
464 Func<object, object> objectToKeyValuePairGetValue = MakeGenericMethod(s_objectToKeyValuePairGetValue, keyType2, valueType2).CreateDelegate<Func<object, object>>();
465 if (collectionContract.Kind == CollectionKind.GenericDictionary)
466 {
467 return delegate(object resultCollection, object collectionItem, int index)
468 {
471 collectionContract.AddMethod.Invoke(resultCollection, new object[2] { obj, obj2 });
472 return resultCollection;
473 };
474 }
475 return delegate(object resultCollection, object collectionItem, int index)
476 {
481 return resultCollection;
482 };
483 }
484 Type type = resultCollectionObject.GetType();
487 if (typeFromHandle.IsAssignableFrom(type))
488 {
489 return delegate(object resultCollection, object collectionItem, int index)
490 {
492 return resultCollection;
493 };
494 }
495 if (typeOfIList.IsAssignableFrom(type))
496 {
497 return delegate(object resultCollection, object collectionItem, int index)
498 {
500 return resultCollection;
501 };
502 }
504 if (addMethod == null)
505 {
507 }
508 return delegate(object resultCollection, object collectionItem, int index)
509 {
510 addMethod.Invoke(resultCollection, new object[1] { collectionItem });
511 return resultCollection;
512 };
513 [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2060:MakeGenericMethod", Justification = "The call to MakeGenericMethod is safe due to the fact that ObjectToKeyValuePairGetKey and ObjectToKeyValuePairGetValue are not annotated.")]
514 static MethodInfo MakeGenericMethod(MethodInfo method, Type keyType, Type valueType)
515 {
516 return method.MakeGenericMethod(keyType, valueType);
517 }
518 }
519
520 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
522 {
523 resultArray = null;
525 if (primitiveDataContract == null)
526 {
527 return false;
528 }
529 switch (itemType.GetTypeCode())
530 {
531 case TypeCode.Boolean:
532 {
533 if (xmlReader.TryReadBooleanArray(context, collectionItemName, collectionItemNamespace, arraySize, out var array6))
534 {
536 }
537 break;
538 }
539 case TypeCode.DateTime:
540 {
541 if (xmlReader.TryReadDateTimeArray(context, collectionItemName, collectionItemNamespace, arraySize, out var array2))
542 {
544 }
545 break;
546 }
547 case TypeCode.Decimal:
548 {
549 if (xmlReader.TryReadDecimalArray(context, collectionItemName, collectionItemNamespace, arraySize, out var array4))
550 {
552 }
553 break;
554 }
555 case TypeCode.Int32:
556 {
558 {
560 }
561 break;
562 }
563 case TypeCode.Int64:
564 {
566 {
568 }
569 break;
570 }
571 case TypeCode.Single:
572 {
573 if (xmlReader.TryReadSingleArray(context, collectionItemName, collectionItemNamespace, arraySize, out var array3))
574 {
576 }
577 break;
578 }
579 case TypeCode.Double:
580 {
581 if (xmlReader.TryReadDoubleArray(context, collectionItemName, collectionItemNamespace, arraySize, out var array))
582 {
584 }
585 break;
586 }
587 default:
588 return false;
589 }
590 return resultArray != null;
591 }
592}
static ? object CreateInstance([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors|DynamicallyAccessedMemberTypes.NonPublicConstructors)] Type type, BindingFlags bindingAttr, Binder? binder, object?[]? args, CultureInfo? culture)
Definition Activator.cs:17
virtual void OnDeserialization(object? sender)
void Add(TKey key, TValue value)
static int GetId(RuntimeTypeHandle typeHandle)
static string GetClrTypeFullName(Type type)
static MemoryStream GetMemoryStream(MemoryStreamAdapter value)
static PrimitiveDataContract GetPrimitiveDataContract(Type type)
void ReflectionReadMembers(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context, XmlDictionaryString[] memberNames, XmlDictionaryString[] memberNamespaces, ClassDataContract classContract, ref object obj)
object ReflectionReadDictionaryItem(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context, CollectionDataContract collectionContract)
string GetCollectionContractNamespace(CollectionDataContract collectionContract)
object ReflectionReadValue(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context, Type type, string name, string ns, PrimitiveDataContract primitiveContractForOriginalType=null)
static object ReadISerializable(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context, ClassDataContract classContract)
object ReflectionReadCollection(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context, XmlDictionaryString collectionItemName, XmlDictionaryString collectionItemNamespace, CollectionDataContract collectionContract)
object ReflectionReadClass(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context, XmlDictionaryString[] memberNames, XmlDictionaryString[] memberNamespaces, ClassDataContract classContract)
CollectionReadItemDelegate GetCollectionReadItemDelegate(CollectionDataContract collectionContract)
object ReflectionCreateCollection(CollectionDataContract collectionContract)
object ReflectionInternalDeserialize(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context, CollectionDataContract collectionContract, Type type, string name, string ns)
static object ResolveAdapterObject(object obj, ClassDataContract classContract)
bool IsArrayLikeInterface(CollectionDataContract collectionContract)
void InvokeOnDeserialized(XmlObjectSerializerReadContext context, ClassDataContract classContract, object obj)
int ReflectionGetMembers(ClassDataContract classContract, DataMember[] members)
static object CreateObject(ClassDataContract classContract)
object ReflectionReadCollectionCore(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context, XmlDictionaryString collectionItemName, XmlDictionaryString collectionItemNamespace, CollectionDataContract collectionContract)
static readonly MethodInfo s_objectToKeyValuePairGetKey
bool IsArrayLikeCollection(CollectionDataContract collectionContract)
void ReflectionSetMemberValue(ref object obj, object memberValue, DataMember dataMember)
bool ReflectionTryReadPrimitiveArray(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context, XmlDictionaryString collectionItemName, XmlDictionaryString collectionItemNamespace, Type type, Type itemType, int arraySize, [NotNullWhen(true)] out object resultArray)
void ReflectionReadMember(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context, ClassDataContract classContract, ref object obj, int memberIndex, DataMember[] members)
object ReflectionReadValue(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context, DataMember dataMember, string ns)
string GetClassContractNamespace(ClassDataContract classContract)
string GetCollectionContractItemName(CollectionDataContract collectionContract)
object ReflectionGetMemberValue(object obj, DataMember dataMember)
static readonly MethodInfo s_objectToKeyValuePairGetValue
void InvokeOnDeserializing(XmlObjectSerializerReadContext context, ClassDataContract classContract, object obj)
object ReadCollectionItems(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context, XmlDictionaryString collectionItemName, XmlDictionaryString collectionItemNamespace, CollectionDataContract collectionContract, object resultCollection, bool isReadOnlyCollection)
void ReflectionReadGetOnlyCollection(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context, XmlDictionaryString collectionItemName, XmlDictionaryString collectionItemNamespace, CollectionDataContract collectionContract)
CollectionReadItemDelegate GetReflectionReadValueDelegate(Type type)
virtual bool ReflectionReadSpecialCollection(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context, CollectionDataContract collectionContract, object resultCollection)
object ReadItemOfPrimitiveType(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context, Type type, string name, string ns, PrimitiveDataContract primitiveContract, int nullables)
delegate object CollectionReadItemDelegate(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context, CollectionDataContract collectionContract, Type itemType, string itemName, string itemNs)
delegate object CollectionSetItemDelegate(object resultCollection, object collectionItem, int itemIndex)
static object UnsafeGetUninitializedObject([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors|DynamicallyAccessedMemberTypes.NonPublicConstructors)] Type type)
object GetExistingObject(string id, Type type, string name, string ns)
string ReadIfNullOrRef(XmlReaderDelegator xmlReader, Type memberType, bool isMemberTypeSerializable)
virtual object InternalDeserialize(XmlReaderDelegator xmlReader, int id, RuntimeTypeHandle declaredTypeHandle, string name, string ns)
SerializationInfo ReadSerializationInfo(XmlReaderDelegator xmlReader, Type type)
static string ValueTypeCannotHaveId
Definition SR.cs:302
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string CollectionMustHaveAddMethod
Definition SR.cs:36
static string ValueTypeCannotHaveRef
Definition SR.cs:306
static string ValueTypeCannotBeNull
Definition SR.cs:298
Definition SR.cs:7
static ? Type GetType(string typeName, bool throwOnError, bool ignoreCase)
Definition Type.cs:408
static readonly Type[] EmptyTypes
Definition Type.cs:19
TypeCode
Definition TypeCode.cs:4
static DateTimeOffset GetDateTimeOffset(DateTimeOffsetAdapter value)