DescriptorBase.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc.
  3. // http://code.google.com/p/protobuf/
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. using Google.ProtocolBuffers.DescriptorProtos;
  17. namespace Google.ProtocolBuffers.Descriptors {
  18. /// <summary>
  19. /// Base class for nearly all descriptors, providing common functionality.
  20. /// </summary>
  21. /// <typeparam name="TProto">Type of the protocol buffer form of this descriptor</typeparam>
  22. /// <typeparam name="TOptions">Type of the options protocol buffer for this descriptor</typeparam>
  23. public abstract class DescriptorBase<TProto, TOptions> : IDescriptor<TProto>
  24. where TProto : IMessage, IDescriptorProto<TOptions> {
  25. private readonly TProto proto;
  26. private readonly FileDescriptor file;
  27. private readonly string fullName;
  28. protected DescriptorBase(TProto proto, FileDescriptor file, string fullName) {
  29. this.proto = proto;
  30. this.file = file;
  31. this.fullName = fullName;
  32. }
  33. protected static string ComputeFullName(FileDescriptor file, MessageDescriptor parent, string name) {
  34. if (parent != null) {
  35. return parent.FullName + "." + name;
  36. }
  37. if (file.Package.Length > 0) {
  38. return file.Package + "." + name;
  39. }
  40. return name;
  41. }
  42. IMessage IDescriptor.Proto {
  43. get { return proto; }
  44. }
  45. /// <summary>
  46. /// Returns the protocol buffer form of this descriptor
  47. /// </summary>
  48. public TProto Proto {
  49. get { return proto; }
  50. }
  51. public TOptions Options {
  52. get { return proto.Options; }
  53. }
  54. /// <summary>
  55. /// The fully qualified name of the descriptor's target.
  56. /// </summary>
  57. public string FullName {
  58. get { return fullName; }
  59. }
  60. /// <summary>
  61. /// The brief name of the descriptor's target.
  62. /// </summary>
  63. public string Name {
  64. get { return proto.Name; }
  65. }
  66. /// <value>
  67. /// The file this descriptor was declared in.
  68. /// </value>
  69. public FileDescriptor File {
  70. get { return file; }
  71. }
  72. }
  73. }