internal void InternalWriteObjectContent(XmlWriterDelegator writer, object?graph, DataContractResolver?dataContractResolver) { if (MaxItemsInObjectGraph == 0) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.Format(SR.ExceededMaxItemsQuota, MaxItemsInObjectGraph))); } DataContract contract = RootContract; Type declaredType = contract.UnderlyingType; Type graphType = (graph == null) ? declaredType : graph.GetType(); if (_serializationSurrogateProvider != null) { graph = SurrogateToDataContractType(_serializationSurrogateProvider, graph, declaredType, ref graphType); } dataContractResolver ??= DataContractResolver; if (graph == null) { if (IsRootXmlAny(_rootName, contract)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.Format(SR.IsAnyCannotBeNull, declaredType))); } WriteNull(writer); } else { if (declaredType == graphType) { if (contract.CanContainReferences) { XmlObjectSerializerWriteContext context = XmlObjectSerializerWriteContext.CreateContext(this, contract, dataContractResolver); context.HandleGraphAtTopLevel(writer, graph, contract); context.SerializeWithoutXsiType(contract, writer, graph, declaredType.TypeHandle); } else { contract.WriteXmlValue(writer, graph, null); } } else { XmlObjectSerializerWriteContext?context; if (IsRootXmlAny(_rootName, contract)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.Format(SR.IsAnyCannotBeSerializedAsDerivedType, graphType, contract.UnderlyingType))); } contract = GetDataContract(contract, declaredType, graphType); context = XmlObjectSerializerWriteContext.CreateContext(this, RootContract, dataContractResolver); if (contract.CanContainReferences) { context.HandleGraphAtTopLevel(writer, graph, contract); } context.OnHandleIsReference(writer, contract, graph); context.SerializeWithXsiTypeAtTopLevel(contract, writer, graph, declaredType.TypeHandle, graphType); } } }
internal void InternalWriteObjectContent(XmlWriterDelegator writer, object graph, System.Runtime.Serialization.DataContractResolver dataContractResolver) { if (this.MaxItemsInObjectGraph == 0) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(System.Runtime.Serialization.SR.GetString("ExceededMaxItemsQuota", new object[] { this.MaxItemsInObjectGraph }))); } DataContract rootContract = this.RootContract; Type underlyingType = rootContract.UnderlyingType; Type objType = (graph == null) ? underlyingType : graph.GetType(); if (this.dataContractSurrogate != null) { graph = SurrogateToDataContractType(this.dataContractSurrogate, graph, underlyingType, ref objType); } if (dataContractResolver == null) { dataContractResolver = this.DataContractResolver; } if (graph == null) { if (base.IsRootXmlAny(this.rootName, rootContract)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(System.Runtime.Serialization.SR.GetString("IsAnyCannotBeNull", new object[] { underlyingType }))); } XmlObjectSerializer.WriteNull(writer); } else if (underlyingType == objType) { if (rootContract.CanContainReferences) { XmlObjectSerializerWriteContext context = XmlObjectSerializerWriteContext.CreateContext(this, rootContract, dataContractResolver); context.HandleGraphAtTopLevel(writer, graph, rootContract); context.SerializeWithoutXsiType(rootContract, writer, graph, underlyingType.TypeHandle); } else { rootContract.WriteXmlValue(writer, graph, null); } } else { XmlObjectSerializerWriteContext context2 = null; if (base.IsRootXmlAny(this.rootName, rootContract)) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(System.Runtime.Serialization.SR.GetString("IsAnyCannotBeSerializedAsDerivedType", new object[] { objType, rootContract.UnderlyingType }))); } rootContract = GetDataContract(rootContract, underlyingType, objType); context2 = XmlObjectSerializerWriteContext.CreateContext(this, this.RootContract, dataContractResolver); if (rootContract.CanContainReferences) { context2.HandleGraphAtTopLevel(writer, graph, rootContract); } context2.OnHandleIsReference(writer, rootContract, graph); context2.SerializeWithXsiTypeAtTopLevel(rootContract, writer, graph, underlyingType.TypeHandle, objType); } }
private void WriteExceptionValue(XmlWriterDelegator writer, object value, XmlObjectSerializerWriteContext context) { /* * Every private field present in System.Exception that is serialized in the thick framework is also serialized in this method. * For classes that inherit from System.Exception all public properties are serialized. * The reason that the Members property is not used here to get the properties to serialize is because Members contains only the properties with setters. */ Type type = value.GetType(); writer.WriteXmlnsAttribute("x", new XmlDictionary(1).Add(Globals.SchemaNamespace)); WriteSystemExceptionRequiredValues(writer, value, context); PropertyInfo[] props = type.GetProperties(); foreach (PropertyInfo prop in props) { //Properties in System.Exception are handled in the call to WriteSystemExceptionRequiredValues, we don't want to repeat these properties. if (PropertyIsSystemExceptionProperty(prop)) { continue; } writer.WriteStartElement(prop.Name, ""); DataContract propDataContract = context.GetDataContract(prop.PropertyType); if (prop.GetValue(value) != null && propDataContract != null && !TryCheckIfNoCountIDictionary(prop.PropertyType, prop.GetValue(value))) { if (!TryWritePrimitive(prop.PropertyType, prop.GetValue(value), writer, context)) { writer.WriteAttributeString(Globals.XsiPrefix, "type", null, "a:" + propDataContract.StableName.Name); writer.WriteXmlnsAttribute("a", new XmlDictionary(1).Add(propDataContract.StableName.Namespace)); context.SerializeWithoutXsiType(propDataContract, writer, prop.GetValue(value), prop.PropertyType.TypeHandle); } } else { writer.WriteAttributeString(Globals.XsiPrefix, "nil", null, "true"); } writer.WriteEndElement(); } }