RepeatedMessageAccessor.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 MethodInfo createBuilderMethod;
  34. internal RepeatedMessageAccessor(string name) : base(name) {
  35. createBuilderMethod = ClrType.GetMethod("CreateBuilder", new Type[0]);
  36. if (createBuilderMethod == null) {
  37. throw new ArgumentException("No public static CreateBuilder method declared in " + ClrType.Name);
  38. }
  39. }
  40. /// <summary>
  41. /// Creates a message of the appropriate CLR type from the given value,
  42. /// which may already be of the right type or may be a dynamic message.
  43. /// </summary>
  44. private object CoerceType(object value) {
  45. // If it's already of the right type, we're done
  46. if (ClrType.IsInstanceOfType(value)) {
  47. return value;
  48. }
  49. // No... so let's create a builder of the right type, and merge the value in.
  50. IMessage message = (IMessage) value;
  51. return CreateBuilder().WeakMergeFrom(message).WeakBuild();
  52. }
  53. public override void SetRepeated(IBuilder builder, int index, object value) {
  54. base.SetRepeated(builder, index, CoerceType(value));
  55. }
  56. public override IBuilder CreateBuilder() {
  57. return (IBuilder) createBuilderMethod.Invoke(null, null);
  58. }
  59. public override void AddRepeated(IBuilder builder, object value) {
  60. base.AddRepeated(builder, CoerceType(value));
  61. }
  62. }
  63. }