public void ReflectionGetOneof()
 {
     TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
     reflectionTester.SetAllFieldsViaReflection(builder);
     Descriptors.OneofDescriptor oneof = TestAllTypes.Descriptor.Oneofs[0];
     Descriptors.FieldDescriptor field = TestAllTypes.Descriptor.FindFieldByName("oneof_bytes");
     Assert.AreSame(field, builder.OneofFieldDescriptor(oneof));
 }
        internal FieldDescriptor(FieldDescriptorProto proto, FileDescriptor file,
                                 MessageDescriptor parent, int index, bool isExtension)
            : base(proto, file, ComputeFullName(file, parent, proto.Name), index)
        {
            if (proto.HasType)
            {
                fieldType  = GetFieldTypeFromProtoType(proto.Type);
                mappedType = FieldTypeToMappedTypeMap[fieldType];
            }

            if (FieldNumber <= 0)
            {
                throw new DescriptorValidationException(this,
                                                        "Field numbers must be positive integers.");
            }

            if (isExtension)
            {
                if (!proto.HasExtendee)
                {
                    throw new DescriptorValidationException(this,
                                                            "FieldDescriptorProto.Extendee not set for extension field.");
                }
                containingType = null; // Will be filled in when cross-linking
                if (parent != null)
                {
                    extensionScope = parent;
                }
                else
                {
                    extensionScope = null;
                }
            }
            else
            {
                if (proto.HasExtendee)
                {
                    throw new DescriptorValidationException(this,
                                                            "FieldDescriptorProto.Extendee set for non-extension field.");
                }
                containingType = parent;
                if (proto.HasOneofIndex)
                {
                    if (proto.OneofIndex < 0 || proto.OneofIndex >= parent.Proto.OneofDeclCount)
                    {
                        throw new DescriptorValidationException(this,
                                                                "FieldDescriptorProto.oneof_index is out of range for type " + parent.Name);
                    }
                    containingOneof = parent.Oneofs[proto.OneofIndex];
                    containingOneof.fieldCount++;
                }
                extensionScope = null;
            }

            file.DescriptorPool.AddSymbol(this);
        }
        internal MessageDescriptor(DescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int typeIndex)
            : base(proto, file, ComputeFullName(file, parent, proto.Name), typeIndex)
        {
            containingType = parent;

            oneofs = DescriptorUtil.ConvertAndMakeReadOnly(proto.OneofDeclList,
                                                           (oneof, index) =>
                                                           new OneofDescriptor(oneof, file, this, index));

            nestedTypes = DescriptorUtil.ConvertAndMakeReadOnly(proto.NestedTypeList,
                                                                (type, index) =>
                                                                new MessageDescriptor(type, file, this, index));

            enumTypes = DescriptorUtil.ConvertAndMakeReadOnly(proto.EnumTypeList,
                                                              (type, index) =>
                                                              new EnumDescriptor(type, file, this, index));

            // TODO(jonskeet): Sort fields first?
            fields = DescriptorUtil.ConvertAndMakeReadOnly(proto.FieldList,
                                                           (field, index) =>
                                                           new FieldDescriptor(field, file, this, index, false));

            extensions = DescriptorUtil.ConvertAndMakeReadOnly(proto.ExtensionList,
                                                               (field, index) =>
                                                               new FieldDescriptor(field, file, this, index, true));

            for (int i = 0; i < proto.OneofDeclCount; i++)
            {
                oneofs[i].fields     = new FieldDescriptor[oneofs[i].FieldCount];
                oneofs[i].fieldCount = 0;
            }
            for (int i = 0; i < proto.FieldCount; i++)
            {
                OneofDescriptor oneofDescriptor = fields[i].ContainingOneof;
                if (oneofDescriptor != null)
                {
                    oneofDescriptor.fields[oneofDescriptor.fieldCount++] = fields[i];
                }
            }
            file.DescriptorPool.AddSymbol(this);
        }