EnumDescriptor.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. 
  2. using System;
  3. using System.Collections.Generic;
  4. using Google.ProtocolBuffers.DescriptorProtos;
  5. namespace Google.ProtocolBuffers.Descriptors {
  6. public class EnumDescriptor : IndexedDescriptorBase<EnumDescriptorProto, EnumOptions> {
  7. private readonly MessageDescriptor containingType;
  8. private readonly IList<EnumValueDescriptor> values;
  9. internal EnumDescriptor(EnumDescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int index)
  10. : base(proto, file, ComputeFullName(file, parent, proto.Name), index) {
  11. containingType = parent;
  12. if (proto.ValueCount == 0) {
  13. // We cannot allow enums with no values because this would mean there
  14. // would be no valid default value for fields of this type.
  15. throw new DescriptorValidationException(this, "Enums must contain at least one value.");
  16. }
  17. values = DescriptorUtil.ConvertAndMakeReadOnly(proto.ValueList,
  18. (value, i) => new EnumValueDescriptor(value, file, this, i));
  19. File.DescriptorPool.AddSymbol(this);
  20. }
  21. /// <value>
  22. /// If this is a nested type, get the outer descriptor, otherwise null.
  23. /// </value>
  24. public MessageDescriptor ContainingType {
  25. get { return containingType; }
  26. }
  27. /// <value>
  28. /// An unmodifiable list of defined value descriptors for this enum.
  29. /// </value>
  30. public IList<EnumValueDescriptor> Values {
  31. get { return values; }
  32. }
  33. /// <summary>
  34. /// Finds an enum value by number. If multiple enum values have the
  35. /// same number, this returns the first defined value with that number.
  36. /// </summary>
  37. // TODO(jonskeet): Make internal and use InternalsVisibleTo?
  38. public EnumValueDescriptor FindValueByNumber(int number) {
  39. return File.DescriptorPool.FindEnumValueByNumber(this, number);
  40. }
  41. /// <summary>
  42. /// Finds an enum value by name.
  43. /// </summary>
  44. /// <param name="name">The unqualified name of the value (e.g. "FOO").</param>
  45. /// <returns>The value's descriptor, or null if not found.</returns>
  46. // TODO(jonskeet): Make internal and use InternalsVisibleTo?
  47. public EnumValueDescriptor FindValueByName(string name) {
  48. return File.DescriptorPool.FindSymbol<EnumValueDescriptor>(FullName + "." + name);
  49. }
  50. }
  51. }