FieldAccessorTable.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 Google.ProtocolBuffers.Descriptors;
  18. namespace Google.ProtocolBuffers.FieldAccess {
  19. /// <summary>
  20. /// Provides access to fields in generated messages via reflection.
  21. /// This type is public to allow it to be used by generated messages, which
  22. /// create appropriate instances in the .proto file description class.
  23. /// TODO(jonskeet): See if we can hide it somewhere...
  24. /// </summary>
  25. public sealed class FieldAccessorTable<TMessage, TBuilder>
  26. where TMessage : IMessage<TMessage, TBuilder>
  27. where TBuilder : IBuilder<TMessage, TBuilder> {
  28. readonly IFieldAccessor<TMessage, TBuilder>[] accessors;
  29. readonly MessageDescriptor descriptor;
  30. public MessageDescriptor Descriptor {
  31. get { return descriptor; }
  32. }
  33. /// <summary>
  34. /// Constructs a FieldAccessorTable for a particular message class.
  35. /// Only one FieldAccessorTable should be constructed per class.
  36. /// </summary>
  37. /// <param name="descriptor">The type's descriptor</param>
  38. /// <param name="propertyNames">The Pascal-case names of all the field-based properties in the message.</param>
  39. public FieldAccessorTable(MessageDescriptor descriptor, String[] propertyNames) {
  40. this.descriptor = descriptor;
  41. accessors = new IFieldAccessor<TMessage, TBuilder>[descriptor.Fields.Count];
  42. for (int i=0; i < accessors.Length; i++) {
  43. accessors[i] = CreateAccessor(descriptor.Fields[i], propertyNames[i]);
  44. }
  45. }
  46. /// <summary>
  47. /// Creates an accessor for a single field
  48. /// </summary>
  49. private static IFieldAccessor<TMessage, TBuilder> CreateAccessor(FieldDescriptor field, string name) {
  50. if (field.IsRepeated) {
  51. switch (field.MappedType) {
  52. case MappedType.Message: return new RepeatedMessageAccessor<TMessage, TBuilder>(name);
  53. case MappedType.Enum: return new RepeatedEnumAccessor<TMessage, TBuilder>(field, name);
  54. default: return new RepeatedPrimitiveAccessor<TMessage, TBuilder>(name);
  55. }
  56. } else {
  57. switch (field.MappedType) {
  58. case MappedType.Message: return new SingleMessageAccessor<TMessage, TBuilder>(name);
  59. case MappedType.Enum: return new SingleEnumAccessor<TMessage, TBuilder>(field, name);
  60. default: return new SinglePrimitiveAccessor<TMessage, TBuilder>(name);
  61. }
  62. }
  63. }
  64. internal IFieldAccessor<TMessage, TBuilder> this[FieldDescriptor field] {
  65. get {
  66. if (field.ContainingType != descriptor) {
  67. throw new ArgumentException("FieldDescriptor does not match message type.");
  68. } else if (field.IsExtension) {
  69. // If this type had extensions, it would subclass ExtendableMessage,
  70. // which overrides the reflection interface to handle extensions.
  71. throw new ArgumentException("This type does not have extensions.");
  72. }
  73. return accessors[field.Index];
  74. }
  75. }
  76. }
  77. }