FieldDescriptor.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using Google.ProtocolBuffers.Collections;
  5. namespace Google.ProtocolBuffers.Descriptors {
  6. public class FieldDescriptor {
  7. private FieldDescriptor(DescriptorProtos.FieldDescriptorProto proto,
  8. FileDescriptor file,
  9. MessageDescriptor parent,
  10. int index,
  11. bool isExtension) {
  12. enumType = null;
  13. }
  14. private EnumDescriptor enumType;
  15. public bool IsRequired {
  16. get;
  17. set;
  18. }
  19. public MappedType MappedType { get; set; }
  20. public bool IsRepeated { get; set; }
  21. public FieldType FieldType { get; set; }
  22. public int FieldNumber { get; set; }
  23. public bool IsExtension { get; set; }
  24. public MessageDescriptor ContainingType { get; set; }
  25. public string FullName { get; set; }
  26. public bool IsOptional { get; set; }
  27. public MessageDescriptor MessageType { get; set; }
  28. public MessageDescriptor ExtensionScope { get; set; }
  29. /// <summary>
  30. /// For enum fields, returns the field's type.
  31. /// </summary>
  32. public EnumDescriptor EnumType {
  33. get {
  34. if (MappedType != MappedType.Enum) {
  35. throw new InvalidOperationException("EnumType is only valid for enum fields.");
  36. }
  37. return enumType;
  38. }
  39. }
  40. /// <summary>
  41. /// The default value for this field. For repeated fields
  42. /// this will always be an empty list. For message fields it will
  43. /// always be null. For singular values, it will depend on the descriptor.
  44. /// </summary>
  45. public object DefaultValue {
  46. get { throw new NotImplementedException(); }
  47. }
  48. public string Name {
  49. get { throw new NotImplementedException(); }
  50. }
  51. /// <summary>
  52. /// Immutable mapping from field type to mapped type. Built using the attributes on
  53. /// FieldType values.
  54. /// </summary>
  55. public static readonly IDictionary<FieldType, MappedType> FieldTypeToWireFormatMap = MapFieldTypes();
  56. private static IDictionary<FieldType, MappedType> MapFieldTypes() {
  57. var map = new Dictionary<FieldType, MappedType>();
  58. foreach (FieldInfo field in typeof(FieldType).GetFields(BindingFlags.Static | BindingFlags.Public)) {
  59. FieldType fieldType = (FieldType)field.GetValue(null);
  60. FieldMappingAttribute mapping = (FieldMappingAttribute)field.GetCustomAttributes(typeof(FieldMappingAttribute), false)[0];
  61. map[fieldType] = mapping.MappedType;
  62. }
  63. return Dictionaries.AsReadOnly(map);
  64. }
  65. }
  66. }