void ReadMembers(ClassDataContract classContract, ExtensionDataObject extensionData)
        {
            int memberCount = classContract.MemberNames.Length;

            context.IncrementItemCount(memberCount);

            int memberIndex = -1;

            // JSON intrinsic part.
            BitFlagsGenerator expectedElements = new BitFlagsGenerator(memberCount);

            byte [] requiredElements = new byte [expectedElements.GetLocalCount()];
            SetRequiredElements(classContract, requiredElements);
            SetExpectedElements(expectedElements, 0 /*startIndex*/);

            while (XmlObjectSerializerReadContext.MoveToNextElement(xmlReader))
            {
                int idx;                 // used as in "switch (idx)" in the original source.
                idx = context.GetJsonMemberIndex(xmlReader, memberNames, memberIndex, extensionData);

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

            if (!CheckRequiredElements(expectedElements, requiredElements))
            {
                XmlObjectSerializerReadContextComplexJson.ThrowMissingRequiredMembers(objectLocal, memberNames, expectedElements.LoadArray(), requiredElements);
            }
        }
            private void ReadMembers(ClassDataContract classContract, LocalBuilder extensionDataLocal)
            {
                int memberCount = classContract.MemberNames.Length;

                _ilg.Call(_contextArg, XmlFormatGeneratorStatics.IncrementItemCountMethod, memberCount);

                BitFlagsGenerator expectedElements = new BitFlagsGenerator(memberCount, _ilg, classContract.UnderlyingType.Name + "_ExpectedElements");

                byte[] requiredElements = new byte[expectedElements.GetLocalCount()];
                SetRequiredElements(classContract, requiredElements);
                SetExpectedElements(expectedElements, 0 /*startIndex*/);

                LocalBuilder memberIndexLocal                 = _ilg.DeclareLocal(Globals.TypeOfInt, "memberIndex", -1);
                Label        throwDuplicateMemberLabel        = _ilg.DefineLabel();
                Label        throwMissingRequiredMembersLabel = _ilg.DefineLabel();

                object forReadElements = _ilg.For(null, null, null);

                _ilg.Call(null, XmlFormatGeneratorStatics.MoveToNextElementMethod, _xmlReaderArg);
                _ilg.IfFalseBreak(forReadElements);
                _ilg.Call(_contextArg, JsonFormatGeneratorStatics.GetJsonMemberIndexMethod, _xmlReaderArg, _memberNamesArg, memberIndexLocal, extensionDataLocal);
                if (memberCount > 0)
                {
                    Label[] memberLabels = _ilg.Switch(memberCount);
                    ReadMembers(classContract, expectedElements, memberLabels, throwDuplicateMemberLabel, memberIndexLocal);
                    _ilg.EndSwitch();
                }
                else
                {
                    _ilg.Pop();
                }
                _ilg.EndFor();
                CheckRequiredElements(expectedElements, requiredElements, throwMissingRequiredMembersLabel);
                Label endOfTypeLabel = _ilg.DefineLabel();

                _ilg.Br(endOfTypeLabel);

                _ilg.MarkLabel(throwDuplicateMemberLabel);
                _ilg.Call(null, JsonFormatGeneratorStatics.ThrowDuplicateMemberExceptionMethod, _objectLocal, _memberNamesArg, memberIndexLocal);

                _ilg.MarkLabel(throwMissingRequiredMembersLabel);
                _ilg.Load(_objectLocal);
                _ilg.ConvertValue(_objectLocal.LocalType, Globals.TypeOfObject);
                _ilg.Load(_memberNamesArg);
                expectedElements.LoadArray();
                LoadArray(requiredElements, "requiredElements");
                _ilg.Call(JsonFormatGeneratorStatics.ThrowMissingRequiredMembersMethod);

                _ilg.MarkLabel(endOfTypeLabel);
            }
            private void ReadMembers(ClassDataContract classContract, LocalBuilder extensionDataLocal)
            {
                int length = classContract.MemberNames.Length;

                this.ilg.Call(this.contextArg, XmlFormatGeneratorStatics.IncrementItemCountMethod, length);
                BitFlagsGenerator expectedElements = new BitFlagsGenerator(length, this.ilg, classContract.UnderlyingType.Name + "_ExpectedElements");

                byte[] requiredElements = new byte[expectedElements.GetLocalCount()];
                this.SetRequiredElements(classContract, requiredElements);
                this.SetExpectedElements(expectedElements, 0);
                LocalBuilder builder = this.ilg.DeclareLocal(Globals.TypeOfInt, "memberIndex", -1);
                Label        throwDuplicateMemberLabel        = this.ilg.DefineLabel();
                Label        throwMissingRequiredMembersLabel = this.ilg.DefineLabel();
                object       forState = this.ilg.For(null, null, null);

                this.ilg.Call(null, XmlFormatGeneratorStatics.MoveToNextElementMethod, this.xmlReaderArg);
                this.ilg.IfFalseBreak(forState);
                this.ilg.Call(this.contextArg, JsonFormatGeneratorStatics.GetJsonMemberIndexMethod, this.xmlReaderArg, this.memberNamesArg, builder, extensionDataLocal);
                if (length > 0)
                {
                    Label[] memberLabels = this.ilg.Switch(length);
                    this.ReadMembers(classContract, expectedElements, memberLabels, throwDuplicateMemberLabel, builder);
                    this.ilg.EndSwitch();
                }
                else
                {
                    this.ilg.Pop();
                }
                this.ilg.EndFor();
                this.CheckRequiredElements(expectedElements, requiredElements, throwMissingRequiredMembersLabel);
                Label label = this.ilg.DefineLabel();

                this.ilg.Br(label);
                this.ilg.MarkLabel(throwDuplicateMemberLabel);
                this.ilg.Call(null, JsonFormatGeneratorStatics.ThrowDuplicateMemberExceptionMethod, this.objectLocal, this.memberNamesArg, builder);
                this.ilg.MarkLabel(throwMissingRequiredMembersLabel);
                this.ilg.Load(this.objectLocal);
                this.ilg.ConvertValue(this.objectLocal.LocalType, Globals.TypeOfObject);
                this.ilg.Load(this.memberNamesArg);
                expectedElements.LoadArray();
                this.LoadArray(requiredElements, "requiredElements");
                this.ilg.Call(JsonFormatGeneratorStatics.ThrowMissingRequiredMembersMethod);
                this.ilg.MarkLabel(label);
            }
            void ReadMembers(ClassDataContract classContract, LocalBuilder extensionDataLocal)
            {
                int memberCount = classContract.MemberNames.Length;
                ilg.Call(contextArg, XmlFormatGeneratorStatics.IncrementItemCountMethod, memberCount);

                BitFlagsGenerator expectedElements = new BitFlagsGenerator(memberCount, ilg, classContract.UnderlyingType.Name + "_ExpectedElements");
                byte[] requiredElements = new byte[expectedElements.GetLocalCount()];
                SetRequiredElements(classContract, requiredElements);
                SetExpectedElements(expectedElements, 0 /*startIndex*/);

                LocalBuilder memberIndexLocal = ilg.DeclareLocal(Globals.TypeOfInt, "memberIndex", -1);
                Label throwDuplicateMemberLabel = ilg.DefineLabel();
                Label throwMissingRequiredMembersLabel = ilg.DefineLabel();

                object forReadElements = ilg.For(null, null, null);
                ilg.Call(null, XmlFormatGeneratorStatics.MoveToNextElementMethod, xmlReaderArg);
                ilg.IfFalseBreak(forReadElements);
                ilg.Call(contextArg, JsonFormatGeneratorStatics.GetJsonMemberIndexMethod, xmlReaderArg, memberNamesArg, memberIndexLocal, extensionDataLocal);
                if (memberCount > 0)
                {
                    Label[] memberLabels = ilg.Switch(memberCount);
                    ReadMembers(classContract, expectedElements, memberLabels, throwDuplicateMemberLabel, memberIndexLocal);
                    ilg.EndSwitch();
                }
                else
                {
                    ilg.Pop();
                }
                ilg.EndFor();
                CheckRequiredElements(expectedElements, requiredElements, throwMissingRequiredMembersLabel);
                Label endOfTypeLabel = ilg.DefineLabel();
                ilg.Br(endOfTypeLabel);

                ilg.MarkLabel(throwDuplicateMemberLabel);
                ilg.Call(null, JsonFormatGeneratorStatics.ThrowDuplicateMemberExceptionMethod, objectLocal, memberNamesArg, memberIndexLocal);

                ilg.MarkLabel(throwMissingRequiredMembersLabel);
                ilg.Load(objectLocal);
                ilg.ConvertValue(objectLocal.LocalType, Globals.TypeOfObject);
                ilg.Load(memberNamesArg);
                expectedElements.LoadArray();
                LoadArray(requiredElements, "requiredElements");
                ilg.Call(JsonFormatGeneratorStatics.ThrowMissingRequiredMembersMethod);

                ilg.MarkLabel(endOfTypeLabel);
            }
		void ReadMembers (ClassDataContract classContract, ExtensionDataObject  extensionData)
		{
			int memberCount = classContract.MemberNames.Length;
			context.IncrementItemCount (memberCount);

			int memberIndex = -1;
			
			// JSON intrinsic part.
			BitFlagsGenerator expectedElements = new BitFlagsGenerator (memberCount);
			byte [] requiredElements = new byte [expectedElements.GetLocalCount ()];
			SetRequiredElements (classContract, requiredElements);
			SetExpectedElements (expectedElements, 0 /*startIndex*/);

			while (XmlObjectSerializerReadContext.MoveToNextElement (xmlReader)) {
				int idx; // used as in "switch (idx)" in the original source.
				idx = context.GetJsonMemberIndex (xmlReader, memberNames, memberIndex, extensionData);

				if (memberCount > 0)
					ReadMembers (idx, classContract, expectedElements, ref memberIndex);
			}

			if (!CheckRequiredElements (expectedElements, requiredElements))
				XmlObjectSerializerReadContextComplexJson.ThrowMissingRequiredMembers (objectLocal, memberNames, expectedElements.LoadArray (), requiredElements);
		}