FieldAccessorTable.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System;
  2. using Google.ProtocolBuffers.Descriptors;
  3. namespace Google.ProtocolBuffers.FieldAccess {
  4. /// <summary>
  5. /// Provides access to fields in generated messages via reflection.
  6. /// This type is public to allow it to be used by generated messages, which
  7. /// create appropriate instances in the .proto file description class.
  8. /// TODO(jonskeet): See if we can hide it somewhere...
  9. /// </summary>
  10. public class FieldAccessorTable {
  11. readonly IFieldAccessor[] accessors;
  12. readonly MessageDescriptor descriptor;
  13. public MessageDescriptor Descriptor {
  14. get { return descriptor; }
  15. }
  16. /// <summary>
  17. /// Constructs a FieldAccessorTable for a particular message class.
  18. /// Only one FieldAccessorTable should be constructed per class.
  19. /// </summary>
  20. /// <param name="descriptor">The type's descriptor</param>
  21. /// <param name="propertyNames">The Pascal-case names of all the field-based properties in the message.</param>
  22. /// <param name="messageType">The .NET type representing the message</param>
  23. /// <param name="builderType">The .NET type representing the message's builder type</param>
  24. public FieldAccessorTable(MessageDescriptor descriptor, String[] propertyNames, Type messageType, Type builderType) {
  25. this.descriptor = descriptor;
  26. accessors = new IFieldAccessor[descriptor.Fields.Count];
  27. for (int i=0; i < accessors.Length; i++) {
  28. accessors[i] = CreateAccessor(descriptor.Fields[i], propertyNames[i], messageType, builderType);
  29. }
  30. }
  31. /// <summary>
  32. /// Creates an accessor for a single field
  33. /// </summary>
  34. private static IFieldAccessor CreateAccessor(FieldDescriptor field, string name, Type messageType, Type builderType) {
  35. if (field.IsRepeated) {
  36. switch (field.MappedType) {
  37. case MappedType.Message: return new RepeatedMessageAccessor(name, messageType, builderType);
  38. case MappedType.Enum: return new RepeatedEnumAccessor(field, name, messageType, builderType);
  39. default: return new RepeatedPrimitiveAccessor(name, messageType, builderType);
  40. }
  41. } else {
  42. switch (field.MappedType) {
  43. case MappedType.Message: return new SingleMessageAccessor(name, messageType, builderType);
  44. case MappedType.Enum: return new SingleEnumAccessor(field, name, messageType, builderType);
  45. default: return new SinglePrimitiveAccessor(name, messageType, builderType);
  46. }
  47. }
  48. }
  49. internal IFieldAccessor this[FieldDescriptor field] {
  50. get {
  51. if (field.ContainingType != descriptor) {
  52. throw new ArgumentException("FieldDescriptor does not match message type.");
  53. } else if (field.IsExtension) {
  54. // If this type had extensions, it would subclass ExtendableMessage,
  55. // which overrides the reflection interface to handle extensions.
  56. throw new ArgumentException("This type does not have extensions.");
  57. }
  58. return accessors[field.Index];
  59. }
  60. }
  61. }
  62. }