MessageDescriptor.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System.Collections.Generic;
  2. using Google.ProtocolBuffers.DescriptorProtos;
  3. namespace Google.ProtocolBuffers.Descriptors {
  4. /// <summary>
  5. /// Describes a message type.
  6. /// </summary>
  7. public class MessageDescriptor : IndexedDescriptorBase<DescriptorProto, MessageOptions> {
  8. private readonly MessageDescriptor containingType;
  9. private readonly IList<MessageDescriptor> nestedTypes;
  10. private readonly IList<EnumDescriptor> enumTypes;
  11. private readonly IList<FieldDescriptor> fields;
  12. private readonly IList<FieldDescriptor> extensions;
  13. internal MessageDescriptor(DescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int typeIndex)
  14. : base(proto, file, ComputeFullName(file, parent, proto.Name), typeIndex) {
  15. containingType = parent;
  16. nestedTypes = DescriptorUtil.ConvertAndMakeReadOnly(proto.NestedTypeList,
  17. (type, index) => new MessageDescriptor(type, file, this, index));
  18. enumTypes = DescriptorUtil.ConvertAndMakeReadOnly(proto.EnumTypeList,
  19. (type, index) => new EnumDescriptor(type, file, this, index));
  20. fields = DescriptorUtil.ConvertAndMakeReadOnly(proto.FieldList,
  21. (field, index) => new FieldDescriptor(field, file, this, index, false));
  22. extensions = DescriptorUtil.ConvertAndMakeReadOnly(proto.ExtensionList,
  23. (field, index) => new FieldDescriptor(field, file, this, index, true));
  24. file.DescriptorPool.AddSymbol(this);
  25. }
  26. /// <value>
  27. /// If this is a nested type, get the outer descriptor, otherwise null.
  28. /// </value>
  29. public MessageDescriptor ContainingType {
  30. get { return containingType; }
  31. }
  32. /// <value>
  33. /// An unmodifiable list of this message type's fields.
  34. /// </value>
  35. public IList<FieldDescriptor> Fields {
  36. get { return fields; }
  37. }
  38. /// <value>
  39. /// An unmodifiable list of this message type's extensions.
  40. /// </value>
  41. public IList<FieldDescriptor> Extensions {
  42. get { return extensions; }
  43. }
  44. /// <value>
  45. /// An unmodifiable list of this message type's nested types.
  46. /// </value>
  47. public IList<MessageDescriptor> NestedTypes {
  48. get { return nestedTypes; }
  49. }
  50. /// <value>
  51. /// An unmodifiable list of this message type's enum types.
  52. /// </value>
  53. public IList<EnumDescriptor> EnumTypes {
  54. get { return enumTypes; }
  55. }
  56. /// <summary>
  57. /// Determines if the given field number is an extension.
  58. /// </summary>
  59. public bool IsExtensionNumber(int number) {
  60. foreach (DescriptorProto.Types.ExtensionRange range in Proto.ExtensionRangeList) {
  61. if (range.Start <= number && number < range.End) {
  62. return true;
  63. }
  64. }
  65. return false;
  66. }
  67. /// <summary>
  68. /// Finds a field by field number.
  69. /// </summary>
  70. /// <param name="number">The field number within this message type.</param>
  71. /// <returns>The field's descriptor, or null if not found.</returns>
  72. public FieldDescriptor FindFieldByNumber(int number) {
  73. return File.DescriptorPool.FindFieldByNumber(this, number);
  74. }
  75. /// <summary>
  76. /// Finds a nested descriptor by name. The is valid for fields, nested
  77. /// message types and enums.
  78. /// </summary>
  79. /// <param name="name">The unqualified name of the descriptor, e.g. "Foo"</param>
  80. /// <returns>The descriptor, or null if not found.</returns>
  81. public T FindDescriptor<T>(string name)
  82. where T : class, IDescriptor {
  83. return File.DescriptorPool.FindSymbol<T>(FullName + "." + name);
  84. }
  85. /// <summary>
  86. /// Looks up and cross-links all fields, nested types, and extensions.
  87. /// </summary>
  88. internal void CrossLink() {
  89. foreach (MessageDescriptor message in nestedTypes) {
  90. message.CrossLink();
  91. }
  92. foreach (FieldDescriptor field in fields) {
  93. field.CrossLink();
  94. }
  95. foreach (FieldDescriptor extension in extensions) {
  96. extension.CrossLink();
  97. }
  98. }
  99. }
  100. }