RepeatedPrimitiveAccessor.cs 4.6 KB

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