GeneratedExtensionBase.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Reflection;
  5. using System.Text;
  6. using Google.ProtocolBuffers.Descriptors;
  7. namespace Google.ProtocolBuffers {
  8. /// <summary>
  9. /// Base type for all generated extensions.
  10. /// </summary>
  11. /// <remarks>
  12. /// The protocol compiler generates a static singleton instance of this
  13. /// class for each extension. For exmaple, imagine a .proto file with:
  14. /// <code>
  15. /// message Foo {
  16. /// extensions 1000 to max
  17. /// }
  18. ///
  19. /// extend Foo {
  20. /// optional int32 bar;
  21. /// }
  22. /// </code>
  23. /// Then MyProto.Foo.Bar has type GeneratedExtensionBase&lt;MyProto.Foo,int&gt;.
  24. /// <para />
  25. /// In general, users should ignore the details of this type, and
  26. /// simply use the static singletons as parameters to the extension accessors
  27. /// in ExtendableMessage and ExtendableBuilder.
  28. /// The interface implemented by both GeneratedException and GeneratedRepeatException,
  29. /// to make it easier to cope with repeats separately.
  30. /// </remarks>
  31. public abstract class GeneratedExtensionBase<TContainer, TExtension> {
  32. private readonly FieldDescriptor descriptor;
  33. private readonly IMessage messageDefaultInstance;
  34. protected GeneratedExtensionBase(FieldDescriptor descriptor, Type singularExtensionType) {
  35. if (!descriptor.IsExtension) {
  36. throw new ArgumentException("GeneratedExtension given a regular (non-extension) field.");
  37. }
  38. this.descriptor = descriptor;
  39. if (descriptor.MappedType == MappedType.Message) {
  40. PropertyInfo defaultInstanceProperty = singularExtensionType
  41. .GetProperty("DefaultInstance", BindingFlags.Static | BindingFlags.Public);
  42. if (defaultInstanceProperty == null) {
  43. throw new ArgumentException("No public static DefaultInstance property for type " + typeof(TExtension).Name);
  44. }
  45. messageDefaultInstance = (IMessage)defaultInstanceProperty.GetValue(null, null);
  46. }
  47. }
  48. public FieldDescriptor Descriptor {
  49. get { return descriptor; }
  50. }
  51. /// <summary>
  52. /// Returns the default message instance for extensions which are message types.
  53. /// </summary>
  54. public IMessage MessageDefaultInstance {
  55. get { return messageDefaultInstance; }
  56. }
  57. public object SingularFromReflectionType(object value) {
  58. switch (Descriptor.MappedType) {
  59. case MappedType.Message:
  60. if (value is TExtension) {
  61. return value;
  62. } else {
  63. // It seems the copy of the embedded message stored inside the
  64. // extended message is not of the exact type the user was
  65. // expecting. This can happen if a user defines a
  66. // GeneratedExtension manually and gives it a different type.
  67. // This should not happen in normal use. But, to be nice, we'll
  68. // copy the message to whatever type the caller was expecting.
  69. return MessageDefaultInstance.CreateBuilderForType()
  70. .MergeFrom((IMessage)value).Build();
  71. }
  72. case MappedType.Enum:
  73. // Just return a boxed int - that can be unboxed to the enum
  74. return ((EnumValueDescriptor) value).Number;
  75. default:
  76. return value;
  77. }
  78. }
  79. /// <summary>
  80. /// Converts from the type used by the native accessors to the type
  81. /// used by reflection accessors. For example, the reflection accessors
  82. /// for enums use EnumValueDescriptors but the native accessors use
  83. /// the generated enum type.
  84. /// </summary>
  85. public object ToReflectionType(object value) {
  86. if (descriptor.IsRepeated) {
  87. if (descriptor.MappedType == MappedType.Enum) {
  88. // Must convert the whole list.
  89. IList<object> result = new List<object>();
  90. foreach (object element in (IEnumerable) value) {
  91. result.Add(SingularToReflectionType(element));
  92. }
  93. return result;
  94. } else {
  95. return value;
  96. }
  97. } else {
  98. return SingularToReflectionType(value);
  99. }
  100. }
  101. /// <summary>
  102. /// Like ToReflectionType(object) but for a single element.
  103. /// </summary>
  104. internal Object SingularToReflectionType(object value) {
  105. return descriptor.MappedType == MappedType.Enum
  106. ? descriptor.EnumType.FindValueByNumber((int) value)
  107. : value;
  108. }
  109. public abstract object FromReflectionType(object value);
  110. }
  111. }