RepeatedMessageAccessor.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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<TMessage, TBuilder> : RepeatedPrimitiveAccessor<TMessage, TBuilder>
  26. where TMessage : IMessage<TMessage, TBuilder>
  27. where TBuilder : IBuilder<TMessage, TBuilder> {
  28. /// <summary>
  29. /// The static method to create a builder for the property type. For example,
  30. /// in a message type "Foo", a field called "bar" might be of type "Baz". This
  31. /// method is Baz.CreateBuilder.
  32. /// </summary>
  33. private readonly Func<IBuilder> createBuilderDelegate;
  34. internal RepeatedMessageAccessor(string name) : base(name) {
  35. MethodInfo createBuilderMethod = ClrType.GetMethod("CreateBuilder", Type.EmptyTypes);
  36. if (createBuilderMethod == null) {
  37. throw new ArgumentException("No public static CreateBuilder method declared in " + ClrType.Name);
  38. }
  39. createBuilderDelegate = ReflectionUtil.CreateStaticUpcastDelegate(createBuilderMethod);
  40. }
  41. /// <summary>
  42. /// Creates a message of the appropriate CLR type from the given value,
  43. /// which may already be of the right type or may be a dynamic message.
  44. /// </summary>
  45. private object CoerceType(object value) {
  46. // If it's already of the right type, we're done
  47. if (ClrType.IsInstanceOfType(value)) {
  48. return value;
  49. }
  50. // No... so let's create a builder of the right type, and merge the value in.
  51. IMessage message = (IMessage) value;
  52. return CreateBuilder().WeakMergeFrom(message).WeakBuild();
  53. }
  54. public override void SetRepeated(TBuilder builder, int index, object value) {
  55. base.SetRepeated(builder, index, CoerceType(value));
  56. }
  57. public override IBuilder CreateBuilder() {
  58. return createBuilderDelegate();
  59. }
  60. public override void AddRepeated(TBuilder builder, object value) {
  61. base.AddRepeated(builder, CoerceType(value));
  62. }
  63. }
  64. }