protected override void ReflectionReadMembers(XmlReaderDelegator xmlReader, XmlObjectSerializerReadContext context, XmlDictionaryString[] memberNames, XmlDictionaryString[]?memberNamespaces, ClassDataContract classContract, ref object obj)
        {
            Debug.Assert(memberNamespaces != null);

            int memberCount = classContract.MemberNames !.Length;

            context.IncrementItemCount(memberCount);
            int memberIndex = -1;
            int firstRequiredMember;

            _ = GetRequiredMembers(classContract, out firstRequiredMember);
            bool hasRequiredMembers = (firstRequiredMember < memberCount);
            int  requiredIndex      = hasRequiredMembers ? firstRequiredMember : -1;

            DataMember[] members = new DataMember[memberCount];
            int          reflectedMemberCount = ReflectionGetMembers(classContract, members);

            Debug.Assert(reflectedMemberCount == memberCount, "The value returned by ReflectionGetMembers() should equal to memberCount.");
            ExtensionDataObject?extensionData = null;

            if (classContract.HasExtensionData)
            {
                extensionData = new ExtensionDataObject();
                ((IExtensibleDataObject)obj).ExtensionData = extensionData;
            }

            while (true)
            {
                if (!XmlObjectSerializerReadContext.MoveToNextElement(xmlReader))
                {
                    return;
                }
                if (hasRequiredMembers)
                {
                    memberIndex = context.GetMemberIndexWithRequiredMembers(xmlReader, memberNames, memberNamespaces, memberIndex, requiredIndex, extensionData);
                }
                else
                {
                    memberIndex = context.GetMemberIndex(xmlReader, memberNames, memberNamespaces, memberIndex, extensionData);
                }

                // GetMemberIndex returns memberNames.Length if member not found
                if (memberIndex < members.Length)
                {
                    ReflectionReadMember(xmlReader, context, classContract, ref obj, memberIndex, members);
                    requiredIndex = memberIndex + 1;
                }
            }
        }
        void ReadMembers(ClassDataContract classContract, ExtensionDataObject extensionData)
        {
            int memberCount = classContract.MemberNames.Length;

            context.IncrementItemCount(memberCount);

            int memberIndex = -1;

            int firstRequiredMember;

            bool[] requiredMembers    = GetRequiredMembers(classContract, out firstRequiredMember);
            bool   hasRequiredMembers = (firstRequiredMember < memberCount);
            int    requiredIndex      = hasRequiredMembers ? firstRequiredMember : memberCount;

            while (XmlObjectSerializerReadContext.MoveToNextElement(xmlReader))
            {
                int idx;                 // used as in "switch (idx)" in the original source.
                if (hasRequiredMembers)
                {
                    idx = context.GetMemberIndexWithRequiredMembers(xmlReader, memberNames, memberNamespaces, memberIndex, (int)requiredIndex, extensionData);
                }
                else
                {
                    idx = context.GetMemberIndex(xmlReader, memberNames, memberNamespaces, memberIndex, extensionData);
                }

                if (memberCount > 0)
                {
                    ReadMembers(idx, classContract, requiredMembers, ref memberIndex, ref requiredIndex);
                }
            }

            if (hasRequiredMembers)
            {
                if (requiredIndex < memberCount)
                {
                    XmlObjectSerializerReadContext.ThrowRequiredMemberMissingException(xmlReader, memberIndex, requiredIndex, memberNames);
                }
            }
        }