RepeatedMessageAccessor.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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.Reflection;
  18. namespace Google.ProtocolBuffers.FieldAccess {
  19. /// <summary>
  20. /// Accessor for a repeated message field.
  21. ///
  22. /// TODO(jonskeet): Try to extract the commonality between this and SingleMessageAccessor.
  23. /// We almost want multiple inheritance...
  24. /// </summary>
  25. internal sealed class RepeatedMessageAccessor : RepeatedPrimitiveAccessor {
  26. /// <summary>
  27. /// The static method to create a builder for the property type. For example,
  28. /// in a message type "Foo", a field called "bar" might be of type "Baz". This
  29. /// method is Baz.CreateBuilder.
  30. /// </summary>
  31. private readonly MethodInfo createBuilderMethod;
  32. internal RepeatedMessageAccessor(string name, Type messageType, Type builderType)
  33. : base(name, messageType, builderType) {
  34. createBuilderMethod = ClrType.GetMethod("CreateBuilder", new Type[0]);
  35. if (createBuilderMethod == null) {
  36. throw new ArgumentException("No public static CreateBuilder method declared in " + ClrType.Name);
  37. }
  38. }
  39. /// <summary>
  40. /// Creates a message of the appropriate CLR type from the given value,
  41. /// which may already be of the right type or may be a dynamic message.
  42. /// </summary>
  43. private object CoerceType(object value) {
  44. // If it's already of the right type, we're done
  45. if (ClrType.IsInstanceOfType(value)) {
  46. return value;
  47. }
  48. // No... so let's create a builder of the right type, and merge the value in.
  49. IMessage message = (IMessage) value;
  50. return CreateBuilder().WeakMergeFrom(message).WeakBuild();
  51. }
  52. public override void SetRepeated(IBuilder builder, int index, object value) {
  53. base.SetRepeated(builder, index, CoerceType(value));
  54. }
  55. public override IBuilder CreateBuilder() {
  56. return (IBuilder) createBuilderMethod.Invoke(null, null);
  57. }
  58. public override void AddRepeated(IBuilder builder, object value) {
  59. base.AddRepeated(builder, CoerceType(value));
  60. }
  61. }
  62. }