FieldAccessorTable.cs 3.6 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 {
  26. readonly IFieldAccessor[] accessors;
  27. readonly MessageDescriptor descriptor;
  28. public MessageDescriptor Descriptor {
  29. get { return descriptor; }
  30. }
  31. /// <summary>
  32. /// Constructs a FieldAccessorTable for a particular message class.
  33. /// Only one FieldAccessorTable should be constructed per class.
  34. /// </summary>
  35. /// <param name="descriptor">The type's descriptor</param>
  36. /// <param name="propertyNames">The Pascal-case names of all the field-based properties in the message.</param>
  37. /// <param name="messageType">The .NET type representing the message</param>
  38. /// <param name="builderType">The .NET type representing the message's builder type</param>
  39. public FieldAccessorTable(MessageDescriptor descriptor, String[] propertyNames, Type messageType, Type builderType) {
  40. this.descriptor = descriptor;
  41. accessors = new IFieldAccessor[descriptor.Fields.Count];
  42. for (int i=0; i < accessors.Length; i++) {
  43. accessors[i] = CreateAccessor(descriptor.Fields[i], propertyNames[i], messageType, builderType);
  44. }
  45. }
  46. /// <summary>
  47. /// Creates an accessor for a single field
  48. /// </summary>
  49. private static IFieldAccessor CreateAccessor(FieldDescriptor field, string name, Type messageType, Type builderType) {
  50. if (field.IsRepeated) {
  51. switch (field.MappedType) {
  52. case MappedType.Message: return new RepeatedMessageAccessor(name, messageType, builderType);
  53. case MappedType.Enum: return new RepeatedEnumAccessor(field, name, messageType, builderType);
  54. default: return new RepeatedPrimitiveAccessor(name, messageType, builderType);
  55. }
  56. } else {
  57. switch (field.MappedType) {
  58. case MappedType.Message: return new SingleMessageAccessor(name, messageType, builderType);
  59. case MappedType.Enum: return new SingleEnumAccessor(field, name, messageType, builderType);
  60. default: return new SinglePrimitiveAccessor(name, messageType, builderType);
  61. }
  62. }
  63. }
  64. internal IFieldAccessor 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. }