RepeatedPrimitiveAccessor.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 System;
  17. using System.Collections;
  18. using System.Reflection;
  19. namespace Google.ProtocolBuffers.FieldAccess {
  20. /// <summary>
  21. /// Accesor for a repeated field of type int, ByteString etc.
  22. /// </summary>
  23. internal class RepeatedPrimitiveAccessor : IFieldAccessor {
  24. private readonly PropertyInfo messageProperty;
  25. private readonly PropertyInfo builderProperty;
  26. private readonly PropertyInfo countProperty;
  27. private readonly MethodInfo clearMethod;
  28. private readonly MethodInfo addMethod;
  29. private readonly MethodInfo getElementMethod;
  30. private readonly MethodInfo setElementMethod;
  31. /// <summary>
  32. /// The CLR type of the field (int, the enum type, ByteString, the message etc).
  33. /// This is taken from the return type of the method used to retrieve a single
  34. /// value.
  35. /// </summary>
  36. protected Type ClrType {
  37. get { return getElementMethod.ReturnType; }
  38. }
  39. internal RepeatedPrimitiveAccessor(string name, Type messageType, Type builderType) {
  40. messageProperty = messageType.GetProperty(name + "List");
  41. builderProperty = builderType.GetProperty(name + "List");
  42. countProperty = messageType.GetProperty(name + "Count");
  43. clearMethod = builderType.GetMethod("Clear" + name);
  44. getElementMethod = messageType.GetMethod("Get" + name, new Type[] { typeof(int) });
  45. addMethod = builderType.GetMethod("Add" + name, new Type[] { ClrType });
  46. setElementMethod = builderType.GetMethod("Set" + name, new Type[] { typeof(int), ClrType });
  47. if (messageProperty == null
  48. || builderProperty == null
  49. || countProperty == null
  50. || clearMethod == null
  51. || addMethod == null
  52. || getElementMethod == null
  53. || setElementMethod == null) {
  54. throw new ArgumentException("Not all required properties/methods available");
  55. }
  56. }
  57. public bool Has(IMessage message) {
  58. throw new InvalidOperationException();
  59. }
  60. public virtual IBuilder CreateBuilder() {
  61. throw new InvalidOperationException();
  62. }
  63. public virtual object GetValue(IMessage message) {
  64. return messageProperty.GetValue(message, null);
  65. }
  66. public void SetValue(IBuilder builder, object value) {
  67. // Add all the elements individually. This serves two purposes:
  68. // 1) Verifies that each element has the correct type.
  69. // 2) Insures that the caller cannot modify the list later on and
  70. // have the modifications be reflected in the message.
  71. Clear(builder);
  72. foreach (object element in (IEnumerable) value) {
  73. AddRepeated(builder, element);
  74. }
  75. }
  76. public void Clear(IBuilder builder) {
  77. clearMethod.Invoke(builder, null);
  78. }
  79. public int GetRepeatedCount(IMessage message) {
  80. return (int) countProperty.GetValue(message, null);
  81. }
  82. public virtual object GetRepeatedValue(IMessage message, int index) {
  83. return getElementMethod.Invoke(message, new object[] {index } );
  84. }
  85. public virtual void SetRepeated(IBuilder builder, int index, object value) {
  86. setElementMethod.Invoke(builder, new object[] {index, value} );
  87. }
  88. public virtual void AddRepeated(IBuilder builder, object value) {
  89. addMethod.Invoke(builder, new object[] { value });
  90. }
  91. /// <summary>
  92. /// The builder class's accessor already builds a read-only wrapper for
  93. /// us, which is exactly what we want.
  94. /// </summary>
  95. public object GetRepeatedWrapper(IBuilder builder) {
  96. return builderProperty.GetValue(builder, null);
  97. }
  98. }
  99. }