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);
            }
        }
        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);
            }

            if (dataContractResolver == null)
            {
                dataContractResolver = this.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 = null;
                    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);
                }
            }
        }