SinglePrimitiveAccessor.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. /// Access for a non-repeated field of a "primitive" type (i.e. not another message or an enum).
  21. /// </summary>
  22. internal class SinglePrimitiveAccessor : IFieldAccessor {
  23. private readonly PropertyInfo messageProperty;
  24. private readonly PropertyInfo builderProperty;
  25. private readonly PropertyInfo hasProperty;
  26. private readonly MethodInfo clearMethod;
  27. /// <summary>
  28. /// The CLR type of the field (int, the enum type, ByteString, the message etc).
  29. /// As declared by the property.
  30. /// </summary>
  31. protected Type ClrType {
  32. get { return messageProperty.PropertyType; }
  33. }
  34. internal SinglePrimitiveAccessor(string name, Type messageType, Type builderType) {
  35. messageProperty = messageType.GetProperty(name);
  36. builderProperty = builderType.GetProperty(name);
  37. hasProperty = messageType.GetProperty("Has" + name);
  38. clearMethod = builderType.GetMethod("Clear" + name);
  39. if (messageProperty == null || builderProperty == null || hasProperty == null || clearMethod == null) {
  40. throw new ArgumentException("Not all required properties/methods available");
  41. }
  42. }
  43. public bool Has(IMessage message) {
  44. return (bool) hasProperty.GetValue(message, null);
  45. }
  46. public void Clear(IBuilder builder) {
  47. clearMethod.Invoke(builder, null);
  48. }
  49. /// <summary>
  50. /// Only valid for message types - this implementation throws InvalidOperationException.
  51. /// </summary>
  52. public virtual IBuilder CreateBuilder() {
  53. throw new InvalidOperationException();
  54. }
  55. public virtual object GetValue(IMessage message) {
  56. return messageProperty.GetValue(message, null);
  57. }
  58. public virtual void SetValue(IBuilder builder, object value) {
  59. builderProperty.SetValue(builder, value, null);
  60. }
  61. #region Methods only related to repeated values
  62. public int GetRepeatedCount(IMessage message) {
  63. throw new InvalidOperationException();
  64. }
  65. public object GetRepeatedValue(IMessage message, int index) {
  66. throw new InvalidOperationException();
  67. }
  68. public void SetRepeated(IBuilder builder, int index, object value) {
  69. throw new InvalidOperationException();
  70. }
  71. public void AddRepeated(IBuilder builder, object value) {
  72. throw new InvalidOperationException();
  73. }
  74. public object GetRepeatedWrapper(IBuilder builder) {
  75. throw new InvalidOperationException();
  76. }
  77. #endregion
  78. }
  79. }