1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- using System;
- using System.Collections.Generic;
- using System.Reflection;
- using Google.ProtocolBuffers.Collections;
- using Google.ProtocolBuffers.DescriptorProtos;
- namespace Google.ProtocolBuffers.Descriptors {
- public class FieldDescriptor : IndexedDescriptorBase<FieldDescriptorProto, FieldOptions> {
- private readonly EnumDescriptor enumType;
- private readonly MessageDescriptor parent;
- internal FieldDescriptor(FieldDescriptorProto proto,
- FileDescriptor file,
- MessageDescriptor parent,
- int index,
- bool isExtension) : base(proto, file, index) {
- enumType = null;
- this.parent = parent;
- }
- public bool IsRequired {
- get;
- set;
- }
- public MappedType MappedType { get; set; }
- public bool IsRepeated { get; set; }
- public FieldType FieldType { get; set; }
- public int FieldNumber { get; set; }
- public bool IsExtension { get; set; }
- public MessageDescriptor ContainingType {
- get { return parent; }
- }
- public bool IsOptional { get; set; }
- public MessageDescriptor MessageType { get; set; }
- public MessageDescriptor ExtensionScope { get; set; }
- /// <summary>
- /// For enum fields, returns the field's type.
- /// </summary>
- public EnumDescriptor EnumType {
- get {
- if (MappedType != MappedType.Enum) {
- throw new InvalidOperationException("EnumType is only valid for enum fields.");
- }
- return enumType;
- }
- }
- /// <summary>
- /// The default value for this field. For repeated fields
- /// this will always be an empty list. For message fields it will
- /// always be null. For singular values, it will depend on the descriptor.
- /// </summary>
- public object DefaultValue {
- get { throw new NotImplementedException(); }
- }
- /// <summary>
- /// Immutable mapping from field type to mapped type. Built using the attributes on
- /// FieldType values.
- /// </summary>
- public static readonly IDictionary<FieldType, MappedType> FieldTypeToWireFormatMap = MapFieldTypes();
- private static IDictionary<FieldType, MappedType> MapFieldTypes() {
- var map = new Dictionary<FieldType, MappedType>();
- foreach (FieldInfo field in typeof(FieldType).GetFields(BindingFlags.Static | BindingFlags.Public)) {
- FieldType fieldType = (FieldType)field.GetValue(null);
- FieldMappingAttribute mapping = (FieldMappingAttribute)field.GetCustomAttributes(typeof(FieldMappingAttribute), false)[0];
- map[fieldType] = mapping.MappedType;
- }
- return Dictionaries.AsReadOnly(map);
- }
- }
- }
|