context: #region Copyright notice and license
  // Protocol Buffers - Google's data interchange format
  // Copyright 2008 Google Inc.  All rights reserved.
  // https://developers.google.com/protocol-buffers/
  //
  // Redistribution and use in source and binary forms, with or without
  // modification, are permitted provided that the following conditions are
  // met:
  //
  //     * Redistributions of source code must retain the above copyright
  // notice, this list of conditions and the following disclaimer.
  //     * Redistributions in binary form must reproduce the above
  // copyright notice, this list of conditions and the following disclaimer
  // in the documentation and/or other materials provided with the
  // distribution.
  //     * Neither the name of Google Inc. nor the names of its
  // contributors may be used to endorse or promote products derived from
  // this software without specific prior written permission.
  //
  // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  #endregion
  
  using System;
  using System.Linq;
  using Google.Protobuf.Compatibility;
  
  namespace Google.Protobuf.Reflection
  {
      /// <summary>
      /// Descriptor for a field or extension within a message in a .proto file.
      /// </summary>
      public sealed class FieldDescriptor : DescriptorBase, IComparable<FieldDescriptor>
      {
          private readonly FieldDescriptorProto proto;
          private EnumDescriptor enumType;
          private MessageDescriptor messageType;
          private readonly MessageDescriptor containingType;
          private readonly OneofDescriptor containingOneof;
          private FieldType fieldType;
          private readonly string propertyName; // Annoyingly, needed in Crosslink.
          private IFieldAccessor accessor;
  
          internal FieldDescriptor(FieldDescriptorProto proto, FileDescriptor file,
                                   MessageDescriptor parent, int index, string propertyName)
              : base(file, file.ComputeFullName(parent, proto.Name), index)
          {
              this.proto = proto;
              if (proto.Type != 0)
              {
                  fieldType = GetFieldTypeFromProtoType(proto.Type);
              }
  
              if (FieldNumber <= 0)
              {
                  throw new DescriptorValidationException(this,
                                                          "Field numbers must be positive integers.");
              }
              containingType = parent;
              // OneofIndex "defaults" to -1 due to a hack in FieldDescriptor.OnConstruction.
              if (proto.OneofIndex != -1)
              {
                  if (proto.OneofIndex < 0 || proto.OneofIndex >= parent.Proto.OneofDecl.Count)
                  {
                      throw new DescriptorValidationException(this,
                          "FieldDescriptorProto.oneof_index is out of range for type " + parent.Name);
                  }
                  containingOneof = parent.Oneofs[proto.OneofIndex];
              }
  
              file.DescriptorPool.AddSymbol(this);
              // We can't create the accessor until we've cross-linked, unfortunately, as we
              // may not know whether the type of the field is a map or not. Remember the property name
              // for later.
              // We could trust the generated code and check whether the type of the property is
              // a MapField, but that feels a tad nasty.
              this.propertyName = propertyName;
          }
  
          /// <summary>
          /// The brief name of the descriptor's target.
          /// </summary>
          public override string Name { get { return proto.Name; } }
  
          internal FieldDescriptorProto Proto { get { return proto; } }
  
          /// <summary>
          /// Returns the accessor for this field, or <c>null</c> if this descriptor does
          /// not support reflective access.
          /// </summary>
          /// <remarks>
          /// While a <see cref="FieldDescriptor"/> describes the field, it does not provide
          /// any way of obtaining or changing the value of the field within a specific message;
          /// that is the responsibility of the accessor.
          /// </remarks>
          public IFieldAccessor Accessor { get { return accessor; } }
          
          /// <summary>
          /// Maps a field type as included in the .proto file to a FieldType.
          /// </summary>
          private static FieldType GetFieldTypeFromProtoType(FieldDescriptorProto.Types.Type type)
          {
              switch (type)
              {
                  case FieldDescriptorProto.Types.Type.TYPE_DOUBLE:
                      return FieldType.Double;
                  case FieldDescriptorProto.Types.Type.TYPE_FLOAT:
                      return FieldType.Float;
                  case FieldDescriptorProto.Types.Type.TYPE_INT64:
                      return FieldType.Int64;
                  case FieldDescriptorProto.Types.Type.TYPE_UINT64:
                      return FieldType.UInt64;
                  case FieldDescriptorProto.Types.Type.TYPE_INT32:
                      return FieldType.Int32;
                  case FieldDescriptorProto.Types.Type.TYPE_FIXED64:
                      return FieldType.Fixed64;
                  case FieldDescriptorProto.Types.Type.TYPE_FIXED32:
                      return FieldType.Fixed32;
                  case FieldDescriptorProto.Types.Type.TYPE_BOOL:
                      return FieldType.Bool;
                  case FieldDescriptorProto.Types.Type.TYPE_STRING:
                      return FieldType.String;
                  case FieldDescriptorProto.Types.Type.TYPE_GROUP:
                      return FieldType.Group;
                  case FieldDescriptorProto.Types.Type.TYPE_MESSAGE:
                      return FieldType.Message;
                  case FieldDescriptorProto.Types.Type.TYPE_BYTES:
                      return FieldType.Bytes;
                  case FieldDescriptorProto.Types.Type.TYPE_UINT32:
                      return FieldType.UInt32;
                  case FieldDescriptorProto.Types.Type.TYPE_ENUM:
                      return FieldType.Enum;
                  case FieldDescriptorProto.Types.Type.TYPE_SFIXED32:
                      return FieldType.SFixed32;
                  case FieldDescriptorProto.Types.Type.TYPE_SFIXED64:
                      return FieldType.SFixed64;
                  case FieldDescriptorProto.Types.Type.TYPE_SINT32:
                      return FieldType.SInt32;
                  case FieldDescriptorProto.Types.Type.TYPE_SINT64:
                      return FieldType.SInt64;
                  default:
                      throw new ArgumentException("Invalid type specified");
              }
          }
  
          /// <summary>
          /// Returns <c>true</c> if this field is a repeated field; <c>false</c> otherwise.
          /// </summary>
          public bool IsRepeated
          {
              get { return Proto.Label == FieldDescriptorProto.Types.Label.LABEL_REPEATED; }
          }
  
          /// <summary>
          /// Returns <c>true</c> if this field is a map field; <c>false</c> otherwise.
          /// </summary>
          public bool IsMap
          {
              get { return fieldType == FieldType.Message && messageType.Proto.Options != null && messageType.Proto.Options.MapEntry; }
          }
  
          /// <summary>
          /// Returns <c>true</c> if this field is a packed, repeated field; <c>false</c> otherwise.
          /// </summary>
          public bool IsPacked
          {
              // Note the || rather than && here - we're effectively defaulting to packed, because that *is*
              // the default in proto3, which is all we support. We may give the wrong result for the protos
              // within descriptor.proto, but that's okay, as they're never exposed and we don't use IsPacked
              // within the runtime.
              get { return Proto.Options == null || Proto.Options.Packed; }
          }        
  
          /// <summary>
          /// Get the field's containing message type.
          /// </summary>
          public MessageDescriptor ContainingType
          {
              get { return containingType; }
          }
  
          /// <summary>
          /// Returns the oneof containing this field, or <c>null</c> if it is not part of a oneof.
          /// </summary>
          public OneofDescriptor ContainingOneof
          {
              get { return containingOneof; }
          }
  
          /// <summary>
          /// Returns  the type of the field.
          /// </summary>
          public FieldType FieldType
          {
              get { return fieldType; }
          }
  
          /// <summary>
          /// Returns the field number declared in the proto file.
          /// </summary>
          public int FieldNumber
          {
  
gt:             get { return Proto.Number; }