Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
XmlObjectSerializerWriteContext.cs
Go to the documentation of this file.
6using System.Xml;
8
10
12{
14
16
17 private const int depthToCheckCyclicReference = 512;
18
20
22
23 private readonly bool _unsafeTypeForwardingEnabled;
24
25 protected bool serializeReadOnlyTypes;
26
28
30 {
31 get
32 {
33 if (_serializedObjects == null)
34 {
36 }
37 return _serializedObjects;
38 }
39 }
40
41 internal override bool IsGetOnlyCollection
42 {
43 get
44 {
46 }
47 set
48 {
50 }
51 }
52
54
56
65
72
78
80 {
82 }
83
85 {
87 }
88
89 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
98
99 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
125
126 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
143
144 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
162
163 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
165 {
166 bool verifyKnownType = false;
169 {
172 {
173 return;
174 }
176 if (!WriteClrTypeInfo(xmlWriter, dataContractSkipValidation) && base.DataContractResolver != null)
177 {
178 if (objectType == null)
179 {
181 }
183 }
184 }
185 else if (declaredType.IsArray)
186 {
190 }
191 else
192 {
195 {
196 return;
197 }
199 {
202 }
203 }
205 }
206
208 {
210 {
211 return false;
212 }
213 bool newId = true;
217 if (newId)
218 {
224 handler.AppendLiteral("i");
225 handler.AppendFormatted(id);
226 xmlWriter.WriteAttributeString("z", idLocalName, serializationNamespace, string.Create(provider, ref handler));
227 return false;
228 }
234 handler2.AppendLiteral("i");
235 handler2.AppendFormatted(id);
236 xmlWriter.WriteAttributeString("z", refLocalName, serializationNamespace2, string.Create(provider2, ref handler2));
237 return true;
238 }
239
240 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
242 {
243 bool flag = false;
244 if (dataContract.KnownDataContracts != null)
245 {
246 scopedKnownTypes.Push(dataContract.KnownDataContracts);
247 flag = true;
248 }
250 {
252 if (dataContract2 == null || dataContract2.UnderlyingType != dataContract.UnderlyingType)
253 {
255 }
256 }
258 if (flag)
259 {
261 }
262 }
263
265 {
266 return false;
267 }
268
270 {
271 return false;
272 }
273
275 {
276 return false;
277 }
278
280 {
281 return false;
282 }
283
284 internal virtual void WriteAnyType(XmlWriterDelegator xmlWriter, object value)
285 {
286 xmlWriter.WriteAnyType(value);
287 }
288
289 internal virtual void WriteString(XmlWriterDelegator xmlWriter, string value)
290 {
291 xmlWriter.WriteString(value);
292 }
293
294 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
296 {
297 if (value == null)
298 {
299 WriteNull(xmlWriter, typeof(string), isMemberTypeSerializable: true, name, ns);
300 return;
301 }
302 xmlWriter.WriteStartElementPrimitive(name, ns);
303 xmlWriter.WriteString(value);
304 xmlWriter.WriteEndElementPrimitive();
305 }
306
307 internal virtual void WriteBase64(XmlWriterDelegator xmlWriter, byte[] value)
308 {
309 xmlWriter.WriteBase64(value);
310 }
311
312 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
314 {
315 if (value == null)
316 {
317 WriteNull(xmlWriter, typeof(byte[]), isMemberTypeSerializable: true, name, ns);
318 return;
319 }
320 xmlWriter.WriteStartElementPrimitive(name, ns);
321 xmlWriter.WriteBase64(value);
322 xmlWriter.WriteEndElementPrimitive();
323 }
324
326 {
327 xmlWriter.WriteUri(value);
328 }
329
330 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
332 {
333 if (value == null)
334 {
336 return;
337 }
338 xmlWriter.WriteStartElementPrimitive(name, ns);
339 xmlWriter.WriteUri(value);
340 xmlWriter.WriteEndElementPrimitive();
341 }
342
344 {
345 xmlWriter.WriteQName(value);
346 }
347
348 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
350 {
351 if (value == null)
352 {
354 return;
355 }
356 if (ns != null && ns.Value != null && ns.Value.Length > 0)
357 {
358 xmlWriter.WriteStartElement("q", name, ns);
359 }
360 else
361 {
362 xmlWriter.WriteStartElement(name, ns);
363 }
364 xmlWriter.WriteQName(value);
365 xmlWriter.WriteEndElement();
366 }
367
369 {
370 writer.WriteXmlnsAttribute("i", DictionaryGlobals.SchemaInstanceNamespace);
371 if (contract.IsISerializable)
372 {
373 writer.WriteXmlnsAttribute("x", DictionaryGlobals.SchemaNamespace);
374 }
376 }
377
394
396 {
397 if (xmlWriter.depth >= 512 && canContainCyclicReference)
398 {
400 }
401 }
402
403 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
409
410 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
417
422
427
432
434 {
435 IncrementItemCount(size);
437 }
438
439 internal virtual void WriteArraySize(XmlWriterDelegator xmlWriter, int size)
440 {
441 }
442
443 internal static bool IsMemberTypeSameAsMemberValue(object obj, Type memberType)
444 {
445 if (obj == null || memberType == null)
446 {
447 return false;
448 }
449 return obj.GetType().TypeHandle.Equals(memberType.TypeHandle);
450 }
451
452 internal static T GetDefaultValue<T>()
453 {
454 return default(T);
455 }
456
457 internal static T GetNullableValue<T>(T? value) where T : struct
458 {
459 return value.Value;
460 }
461
466
467 internal static bool GetHasValue<T>(T? value) where T : struct
468 {
469 return value.HasValue;
470 }
471
480
485
511
512 [MethodImpl(MethodImplOptions.NoInlining)]
517
518 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
530
531 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
533 {
534 if (DataContract.GetClrTypeFullName(objType) != serInfo.FullTypeName)
535 {
536 if (base.DataContractResolver != null)
537 {
538 if (ResolveType(serInfo.ObjectType, objType, out var typeName, out var typeNamespace))
539 {
541 }
542 }
543 else
544 {
545 DataContract.GetDefaultStableName(serInfo.FullTypeName, out var localName, out var ns);
547 }
548 }
550 IncrementItemCount(serInfo.MemberCount);
552 while (enumerator.MoveNext())
553 {
554 SerializationEntry current = enumerator.Current;
557 object value = current.Value;
558 if (value == null)
559 {
561 }
562 else
563 {
565 }
566 xmlWriter.WriteEndElement();
567 }
568 }
569
570 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
575
580
581 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
589
590 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
611
612 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
614 {
616 {
617 if (base.DataContractResolver == null)
618 {
619 WriteTypeInfo(writer, contract.Name, contract.Namespace);
620 return true;
621 }
622 WriteResolvedTypeInfo(writer, contract.OriginalUnderlyingType, declaredContract.OriginalUnderlyingType);
623 return false;
624 }
625 return false;
626 }
627
632
637
638 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
640 {
641 if (base.IgnoreExtensionDataObject || extensionData == null)
642 {
643 return;
644 }
646 if (members == null)
647 {
648 return;
649 }
650 for (int i = 0; i < members.Count; i++)
651 {
653 if (extensionDataMember.MemberIndex == memberIndex)
654 {
656 }
657 }
658 }
659
660 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
662 {
663 xmlWriter.WriteStartElement(member.Name, member.Namespace);
664 IDataNode value = member.Value;
666 xmlWriter.WriteEndElement();
667 }
668
669 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
671 {
672 if (dataNode.DataContractName != null)
673 {
675 }
676 WriteClrTypeInfo(xmlWriter, dataNode.DataType, dataNode.ClrTypeName, dataNode.ClrAssemblyName);
677 }
678
679 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
681 {
683 if (dataNode == null)
684 {
686 }
687 else
688 {
689 if (dataNode.PreservesReferences && OnHandleReference(xmlWriter, (dataNode.Value == null) ? dataNode : dataNode.Value, canContainCyclicReference: true))
690 {
691 return;
692 }
693 Type dataType = dataNode.DataType;
694 if (dataType == Globals.TypeOfClassDataNode)
695 {
697 }
698 else if (dataType == Globals.TypeOfCollectionDataNode)
699 {
701 }
702 else if (dataType == Globals.TypeOfXmlDataNode)
703 {
705 }
706 else if (dataType == Globals.TypeOfISerializableDataNode)
707 {
709 }
710 else
711 {
713 if (dataType == Globals.TypeOfObject)
714 {
715 object value = dataNode.Value;
716 if (value != null)
717 {
718 InternalSerialize(xmlWriter, value, isDeclaredType: false, writeXsiType: false, -1, value.GetType().TypeHandle);
719 }
720 }
721 else
722 {
723 xmlWriter.WriteExtensionData(dataNode);
724 }
725 }
726 if (dataNode.PreservesReferences)
727 {
728 OnEndHandleReference(xmlWriter, (dataNode.Value == null) ? dataNode : dataNode.Value, canContainCyclicReference: true);
729 }
730 }
731 }
732
733 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
735 {
736 object value = dataNode.Value;
737 if (value == null)
738 {
739 return false;
740 }
741 Type type = ((dataNode.DataContractName == null) ? value.GetType() : Globals.TypeOfObject);
742 InternalSerialize(xmlWriter, value, isDeclaredType: false, writeXsiType: false, -1, type.TypeHandle);
743 return true;
744 }
745
746 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
748 {
750 {
751 return;
752 }
755 if (members != null)
756 {
757 for (int i = 0; i < members.Count; i++)
758 {
760 }
761 }
762 }
763
764 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
766 {
768 {
769 return;
770 }
772 WriteArraySize(xmlWriter, dataNode.Size);
773 IList<IDataNode> items = dataNode.Items;
774 if (items != null)
775 {
776 for (int i = 0; i < items.Count; i++)
777 {
778 xmlWriter.WriteStartElement(dataNode.ItemName, dataNode.ItemNamespace);
780 xmlWriter.WriteEndElement();
781 }
782 }
783 }
784
785 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
787 {
789 {
790 return;
791 }
793 if (dataNode.FactoryTypeName != null)
794 {
796 }
798 if (members != null)
799 {
800 for (int i = 0; i < members.Count; i++)
801 {
803 xmlWriter.WriteStartElement(serializableDataMember.Name, string.Empty);
805 xmlWriter.WriteEndElement();
806 }
807 }
808 }
809
810 [RequiresUnreferencedCode("Data Contract Serialization and Deserialization might require types that cannot be statically analyzed. Make sure all of the required types are preserved.")]
812 {
814 {
815 return;
816 }
818 if (xmlAttributes != null)
819 {
820 foreach (XmlAttribute item in xmlAttributes)
821 {
822 item.WriteTo(xmlWriter.Writer);
823 }
824 }
827 if (xmlChildNodes == null)
828 {
829 return;
830 }
831 foreach (XmlNode item2 in xmlChildNodes)
832 {
833 item2.WriteTo(xmlWriter.Writer);
834 }
835 }
836}
virtual void GetObjectData(SerializationInfo info, StreamingContext context)
static CultureInfo InvariantCulture
IList< ExtensionDataMember > Members
static string EncodeLocalName(string localName)
static int GetId(RuntimeTypeHandle typeHandle)
static XmlDictionaryString GetClrTypeString(string key)
static XmlQualifiedName GetDefaultStableName(Type type)
static string GetClrTypeFullName(Type type)
static readonly XmlDictionaryString SchemaNamespace
static readonly XmlDictionaryString RefLocalName
static readonly XmlDictionaryString IdLocalName
static readonly XmlDictionaryString ISerializableFactoryTypeLocalName
static readonly XmlDictionaryString XsiTypeLocalName
static readonly XmlDictionaryString SerializationNamespace
static readonly XmlDictionaryString EmptyString
static readonly XmlDictionaryString SchemaInstanceNamespace
int GetId(object obj, ref bool newId)
virtual DataContract GetDataContractSkipValidation(int typeId, RuntimeTypeHandle typeHandle, Type type)
bool IsKnownType(DataContract dataContract, Dictionary< XmlQualifiedName, DataContract > knownDataContracts, Type declaredType)
DataContract ResolveDataContractFromKnownTypes(XmlQualifiedName typeName)
virtual void CheckIfTypeSerializable(Type memberType, bool isMemberTypeSerializable)
virtual void WriteTypeInfo(XmlWriterDelegator writer, XmlDictionaryString dataContractName, XmlDictionaryString dataContractNamespace)
bool TryWriteDeserializedExtensionData(XmlWriterDelegator xmlWriter, IDataNode dataNode)
void WriteSerializationInfo(XmlWriterDelegator xmlWriter, Type objType, SerializationInfo serInfo)
virtual void WriteQName(XmlWriterDelegator xmlWriter, XmlQualifiedName value)
bool OnHandleIsReference(XmlWriterDelegator xmlWriter, DataContract contract, object obj)
void WriteExtensionCollectionData(XmlWriterDelegator xmlWriter, CollectionDataNode dataNode)
void WriteNull(XmlWriterDelegator xmlWriter, Type memberType, bool isMemberTypeSerializable)
virtual bool WriteClrTypeInfo(XmlWriterDelegator xmlWriter, DataContract dataContract)
virtual bool WriteClrTypeInfo(XmlWriterDelegator xmlWriter, Type dataContractType, string clrTypeName, string clrAssemblyName)
void WriteExtensionDataMember(XmlWriterDelegator xmlWriter, ExtensionDataMember member)
virtual void WriteAnyType(XmlWriterDelegator xmlWriter, object value)
virtual void WriteString(XmlWriterDelegator xmlWriter, string value)
virtual void SerializeWithXsiTypeAtTopLevel(DataContract dataContract, XmlWriterDelegator xmlWriter, object obj, RuntimeTypeHandle originalDeclaredTypeHandle, Type graphType)
virtual void WriteExtensionDataTypeInfo(XmlWriterDelegator xmlWriter, IDataNode dataNode)
bool ResolveType(Type objectType, Type declaredType, [NotNullWhen(true)] out XmlDictionaryString typeName, [NotNullWhen(true)] out XmlDictionaryString typeNamespace)
virtual void WriteBase64(XmlWriterDelegator xmlWriter, byte[] value)
virtual bool WriteTypeInfo(XmlWriterDelegator writer, DataContract contract, DataContract declaredContract)
virtual bool OnHandleReference(XmlWriterDelegator xmlWriter, object obj, bool canContainCyclicReference)
virtual void WriteString(XmlWriterDelegator xmlWriter, string value, XmlDictionaryString name, XmlDictionaryString ns)
void WriteISerializable(XmlWriterDelegator xmlWriter, ISerializable obj)
virtual bool WriteClrTypeInfo(XmlWriterDelegator xmlWriter, Type dataContractType, SerializationInfo serInfo)
void WriteNull(XmlWriterDelegator xmlWriter, Type memberType, bool isMemberTypeSerializable, XmlDictionaryString name, XmlDictionaryString ns)
virtual void WriteArraySize(XmlWriterDelegator xmlWriter, int size)
virtual void WriteQName(XmlWriterDelegator xmlWriter, XmlQualifiedName value, XmlDictionaryString name, XmlDictionaryString ns)
virtual void WriteUri(XmlWriterDelegator xmlWriter, Uri value, XmlDictionaryString name, XmlDictionaryString ns)
void InternalSerializeReference(XmlWriterDelegator xmlWriter, object obj, bool isDeclaredType, bool writeXsiType, int declaredTypeID, RuntimeTypeHandle declaredTypeHandle)
void IncrementCollectionCount(XmlWriterDelegator xmlWriter, ICollection collection)
virtual void OnEndHandleReference(XmlWriterDelegator xmlWriter, object obj, bool canContainCyclicReference)
XmlObjectSerializerWriteContext(DataContractSerializer serializer, DataContract rootTypeDataContract, DataContractResolver resolver)
static void WriteRootIXmlSerializable(XmlWriterDelegator xmlWriter, object obj)
void WriteResolvedTypeInfo(XmlWriterDelegator writer, Type objectType, Type declaredType)
XmlObjectSerializerWriteContext(XmlObjectSerializer serializer, int maxItemsInObjectGraph, StreamingContext streamingContext, bool ignoreExtensionDataObject)
virtual bool WriteClrTypeInfo(XmlWriterDelegator xmlWriter, string clrTypeName, string clrAssemblyName)
virtual void WriteBase64(XmlWriterDelegator xmlWriter, byte[] value, XmlDictionaryString name, XmlDictionaryString ns)
void SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, object obj, RuntimeTypeHandle declaredTypeHandle)
static XmlObjectSerializerWriteContext CreateContext(DataContractSerializer serializer, DataContract rootTypeDataContract, DataContractResolver dataContractResolver)
virtual void WriteUri(XmlWriterDelegator xmlWriter, Uri value)
virtual void WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, object obj, RuntimeTypeHandle declaredTypeHandle)
void SerializeAndVerifyType(DataContract dataContract, XmlWriterDelegator xmlWriter, object obj, bool verifyKnownType, RuntimeTypeHandle declaredTypeHandle, Type declaredType)
void WriteExtensionISerializableData(XmlWriterDelegator xmlWriter, ISerializableDataNode dataNode)
void WriteExtensionXmlData(XmlWriterDelegator xmlWriter, XmlDataNode dataNode)
static void WriteIXmlSerializable(XmlWriterDelegator xmlWriter, object obj, XmlSerializableWriter xmlSerializableWriter)
void WriteExtensionClassData(XmlWriterDelegator xmlWriter, ClassDataNode dataNode)
void GetObjectData(ISerializable obj, SerializationInfo serInfo, StreamingContext context)
void IncrementCollectionCountGeneric< T >(XmlWriterDelegator xmlWriter, ICollection< T > collection)
virtual void WriteTypeInfo(XmlWriterDelegator writer, string dataContractName, string dataContractNamespace)
void HandleGraphAtTopLevel(XmlWriterDelegator writer, object obj, DataContract contract)
virtual void InternalSerialize(XmlWriterDelegator xmlWriter, object obj, bool isDeclaredType, bool writeXsiType, int declaredTypeID, RuntimeTypeHandle declaredTypeHandle)
virtual void SerializeWithXsiType(XmlWriterDelegator xmlWriter, object obj, RuntimeTypeHandle objectTypeHandle, Type objectType, int declaredTypeID, RuntimeTypeHandle declaredTypeHandle, Type declaredType)
void WriteExtensionData(XmlWriterDelegator xmlWriter, ExtensionDataObject extensionData, int memberIndex)
void WriteExtensionDataValue(XmlWriterDelegator xmlWriter, IDataNode dataNode)
static void WriteNull(XmlWriterDelegator writer)
static bool IsContractDeclared(DataContract contract, DataContract declaredContract)
static SerializationException CreateSerializationException(string errorMessage)
static string ISerializableAssemblyNameSetToZero
Definition SR.cs:656
static string RequiredMemberMustBeEmitted
Definition SR.cs:268
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string ResolveTypeReturnedFalse
Definition SR.cs:270
static string DcTypeNotFoundOnSerialize
Definition SR.cs:66
static string CannotSerializeObjectWithCycles
Definition SR.cs:24
static string UnknownXmlType
Definition SR.cs:638
static string ResolveTypeReturnedNull
Definition SR.cs:272
Definition SR.cs:7
static ? Type GetType(string typeName, bool throwOnError, bool ignoreCase)
Definition Type.cs:408
static Type GetTypeFromHandle(RuntimeTypeHandle handle)
void Push(Dictionary< XmlQualifiedName, DataContract > dataContractDictionary)