protected override int ReflectionWriteMembers(XmlWriterDelegator xmlWriter, object obj, XmlObjectSerializerWriteContext context, ClassDataContract classContract, ClassDataContract derivedMostClassContract, int childElementIndex, XmlDictionaryString[] emptyStringArray)
        {
            int memberCount = (classContract.BaseContract == null) ? 0 :
                              ReflectionWriteMembers(xmlWriter, obj, context, classContract.BaseContract, derivedMostClassContract, childElementIndex, emptyStringArray);

            childElementIndex += memberCount;

            Type classType = classContract.UnadaptedClassType;

            XmlDictionaryString[] memberNames = classContract.MemberNames;
            XmlDictionaryString   ns          = classContract.Namespace;

            context.IncrementItemCount(classContract.Members.Count);
            for (int i = 0; i < classContract.Members.Count; i++, memberCount++)
            {
                DataMember member     = classContract.Members[i];
                Type       memberType = member.MemberType;
                if (member.IsGetOnlyCollection)
                {
                    context.StoreIsGetOnlyCollection();
                }
                else
                {
                    context.ResetIsGetOnlyCollection();
                }

                bool   shouldWriteValue = true;
                object memberValue      = null;
                if (!member.EmitDefaultValue)
                {
                    memberValue = ReflectionGetMemberValue(obj, member);
                    object defaultValue = XmlFormatGeneratorStatics.GetDefaultValue(memberType);
                    if ((memberValue == null && defaultValue == null) ||
                        (memberValue != null && memberValue.Equals(defaultValue)))
                    {
                        shouldWriteValue = false;

                        if (member.IsRequired)
                        {
                            XmlObjectSerializerWriteContext.ThrowRequiredMemberMustBeEmitted(member.Name, classContract.UnderlyingType);
                        }
                    }
                }

                if (shouldWriteValue)
                {
                    bool writeXsiType = CheckIfMemberHasConflict(member, classContract, derivedMostClassContract);
                    if (memberValue == null)
                    {
                        memberValue = ReflectionGetMemberValue(obj, member);
                    }
                    PrimitiveDataContract primitiveContract = member.MemberPrimitiveContract;

                    if (writeXsiType || !ReflectionTryWritePrimitive(xmlWriter, context, memberType, memberValue, memberNames[i + childElementIndex] /*name*/, ns, primitiveContract))
                    {
                        ReflectionWriteStartElement(xmlWriter, memberType, ns, ns.Value, member.Name, 0);
                        if (classContract.ChildElementNamespaces[i + childElementIndex] != null)
                        {
                            var nsChildElement = classContract.ChildElementNamespaces[i + childElementIndex];
                            xmlWriter.WriteNamespaceDecl(nsChildElement);
                        }
                        ReflectionWriteValue(xmlWriter, context, memberType, memberValue, writeXsiType, primitiveContractForParamType: null);
                        ReflectionWriteEndElement(xmlWriter);
                    }

                    if (classContract.HasExtensionData)
                    {
                        context.WriteExtensionData(xmlWriter, ((IExtensibleDataObject)obj).ExtensionData, memberCount);
                    }
                }
            }

            return(memberCount);
        }
        int WriteMembers(ClassDataContract classContract, ExtensionDataObject extensionData, ClassDataContract derivedMostClassContract)
        {
            int memberCount = (classContract.BaseContract == null) ? 0 : WriteMembers(classContract.BaseContract, extensionData, derivedMostClassContract);

            XmlDictionaryString ns =
                (contractNamespaces == null) ? dataContract.Namespace :
                contractNamespaces [typeIndex - 1];

            ctx.IncrementItemCount(classContract.Members.Count);

            for (int i = 0; i < classContract.Members.Count; i++, memberCount++)
            {
                DataMember member      = classContract.Members[i];
                Type       memberType  = member.MemberType;
                object     memberValue = null;
                if (member.IsGetOnlyCollection)
                {
                    ctx.StoreIsGetOnlyCollection();
                }
                bool doWrite = true, hasMemberValue = false;
                if (!member.EmitDefaultValue)
                {
                    hasMemberValue = true;
                    memberValue    = LoadMemberValue(member);
                    doWrite        = !IsDefaultValue(memberType, memberValue);
                }

                if (doWrite)
                {
                    bool writeXsiType = CheckIfMemberHasConflict(member, classContract, derivedMostClassContract);
                    if (writeXsiType || !TryWritePrimitive(memberType, hasMemberValue ? () => memberValue : (Func <object>)null, member.MemberInfo, null /*arrayItemIndex*/, ns, null /*nameLocal*/, i + childElementIndex))
                    {
                        WriteStartElement(memberType, classContract.Namespace, ns, null /*nameLocal*/, i + childElementIndex);
                        if (classContract.ChildElementNamespaces [i + childElementIndex] != null)
                        {
                            writer.WriteNamespaceDecl(childElementNamespaces [i + childElementIndex]);
                        }
                        if (memberValue == null)
                        {
                            memberValue = LoadMemberValue(member);
                        }
                        WriteValue(memberType, memberValue, writeXsiType);
                        WriteEndElement();
                    }

                    if (classContract.HasExtensionData)
                    {
                        ctx.WriteExtensionData(writer, extensionData, memberCount);
                    }
                }
                else if (!member.EmitDefaultValue)
                {
                    if (member.IsRequired)
                    {
                        XmlObjectSerializerWriteContext.ThrowRequiredMemberMustBeEmitted(member.Name, classContract.UnderlyingType);
                    }
                }
            }

            typeIndex++;
            childElementIndex += classContract.Members.Count;
            return(memberCount);
        }