GeneratedExtensionBase.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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<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.WeakCreateBuilderForType()
  70. .WeakMergeFrom((IMessage)value).WeakBuild();
  71. }
  72. case MappedType.Enum:
  73. // Just return a boxed int - that can be unboxed to the enum
  74. EnumValueDescriptor enumValue = (EnumValueDescriptor) value;
  75. return enumValue.Number;
  76. default:
  77. return value;
  78. }
  79. }
  80. /// <summary>
  81. /// Converts from the type used by the native accessors to the type
  82. /// used by reflection accessors. For example, the reflection accessors
  83. /// for enums use EnumValueDescriptors but the native accessors use
  84. /// the generated enum type.
  85. /// </summary>
  86. public object ToReflectionType(object value) {
  87. if (descriptor.IsRepeated) {
  88. if (descriptor.MappedType == MappedType.Enum) {
  89. // Must convert the whole list.
  90. IList<object> result = new List<object>();
  91. foreach (object element in (IEnumerable) value) {
  92. result.Add(SingularToReflectionType(element));
  93. }
  94. return result;
  95. } else {
  96. return value;
  97. }
  98. } else {
  99. return SingularToReflectionType(value);
  100. }
  101. }
  102. /// <summary>
  103. /// Like ToReflectionType(object) but for a single element.
  104. /// </summary>
  105. internal Object SingularToReflectionType(object value) {
  106. return descriptor.MappedType == MappedType.Enum
  107. ? descriptor.EnumType.FindValueByNumber((int) value)
  108. : value;
  109. }
  110. public abstract object FromReflectionType(object value);
  111. }
  112. }