DescriptorBase.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Google.ProtocolBuffers.DescriptorProtos;
  5. using Google.ProtocolBuffers.Collections;
  6. namespace Google.ProtocolBuffers.Descriptors {
  7. /// <summary>
  8. /// Base class for nearly all descriptors, providing common functionality.
  9. /// </summary>
  10. /// <typeparam name="TProto">Type of the protocol buffer form of this descriptor</typeparam>
  11. /// <typeparam name="TOptions">Type of the options protocol buffer for this descriptor</typeparam>
  12. public abstract class DescriptorBase<TProto, TOptions> : IDescriptor<TProto>
  13. where TProto : IMessage, IDescriptorProto<TOptions> {
  14. private readonly TProto proto;
  15. private readonly FileDescriptor file;
  16. private readonly string fullName;
  17. protected DescriptorBase(TProto proto, FileDescriptor file, string fullName) {
  18. this.proto = proto;
  19. this.file = file;
  20. this.fullName = fullName;
  21. }
  22. protected static string ComputeFullName(FileDescriptor file, MessageDescriptor parent, string name) {
  23. if (parent != null) {
  24. return parent.FullName + "." + name;
  25. }
  26. if (file.Package.Length > 0) {
  27. return file.Package + "." + name;
  28. }
  29. return name;
  30. }
  31. IMessage IDescriptor.Proto {
  32. get { return proto; }
  33. }
  34. /// <summary>
  35. /// Returns the protocol buffer form of this descriptor
  36. /// </summary>
  37. public TProto Proto {
  38. get { return proto; }
  39. }
  40. public TOptions Options {
  41. get { return proto.Options; }
  42. }
  43. /// <summary>
  44. /// The fully qualified name of the descriptor's target.
  45. /// </summary>
  46. public string FullName {
  47. get { return fullName; }
  48. }
  49. /// <summary>
  50. /// The brief name of the descriptor's target.
  51. /// </summary>
  52. public string Name {
  53. get { return proto.Name; }
  54. }
  55. /// <value>
  56. /// The file this descriptor was declared in.
  57. /// </value>
  58. public FileDescriptor File {
  59. get { return file; }
  60. }
  61. }
  62. }