SinglePrimitiveAccessor.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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<TMessage, TBuilder> : IFieldAccessor<TMessage, TBuilder>
  23. where TMessage : IMessage<TMessage, TBuilder>
  24. where TBuilder : IBuilder<TMessage, TBuilder> {
  25. private readonly Type clrType;
  26. private readonly Func<TMessage, object> getValueDelegate;
  27. private readonly Action<TBuilder, object> setValueDelegate;
  28. private readonly Func<TMessage, bool> hasDelegate;
  29. private readonly Func<TBuilder, IBuilder> clearDelegate;
  30. /// <summary>
  31. /// The CLR type of the field (int, the enum type, ByteString, the message etc).
  32. /// As declared by the property.
  33. /// </summary>
  34. protected Type ClrType {
  35. get { return clrType; }
  36. }
  37. internal SinglePrimitiveAccessor(string name) {
  38. string propertyName = name == typeof(TMessage).Name ? name + "_" : name;
  39. PropertyInfo messageProperty = typeof(TMessage).GetProperty(propertyName);
  40. PropertyInfo builderProperty = typeof(TBuilder).GetProperty(name); // FIXME!
  41. if (builderProperty == null) builderProperty = typeof(TBuilder).GetProperty(propertyName); // FIXME!
  42. PropertyInfo hasProperty = typeof(TMessage).GetProperty("Has" + name);
  43. MethodInfo clearMethod = typeof(TBuilder).GetMethod("Clear" + name, Type.EmptyTypes);
  44. if (messageProperty == null || builderProperty == null || hasProperty == null || clearMethod == null) {
  45. throw new ArgumentException("Not all required properties/methods available");
  46. }
  47. clrType = messageProperty.PropertyType;
  48. hasDelegate = (Func<TMessage, bool>)Delegate.CreateDelegate(typeof(Func<TMessage, bool>), hasProperty.GetGetMethod());
  49. clearDelegate = (Func<TBuilder, IBuilder>)Delegate.CreateDelegate(typeof(Func<TBuilder, IBuilder>), clearMethod);
  50. getValueDelegate = ReflectionUtil.CreateUpcastDelegate<TMessage>(messageProperty.GetGetMethod());
  51. setValueDelegate = ReflectionUtil.CreateDowncastDelegate<TBuilder>(builderProperty.GetSetMethod());
  52. }
  53. public bool Has(TMessage message) {
  54. return hasDelegate(message);
  55. }
  56. public void Clear(TBuilder builder) {
  57. clearDelegate(builder);
  58. }
  59. /// <summary>
  60. /// Only valid for message types - this implementation throws InvalidOperationException.
  61. /// </summary>
  62. public virtual IBuilder CreateBuilder() {
  63. throw new InvalidOperationException();
  64. }
  65. public virtual object GetValue(TMessage message) {
  66. return getValueDelegate(message);
  67. }
  68. public virtual void SetValue(TBuilder builder, object value) {
  69. setValueDelegate(builder, value);
  70. }
  71. #region Methods only related to repeated values
  72. public int GetRepeatedCount(TMessage message) {
  73. throw new InvalidOperationException();
  74. }
  75. public object GetRepeatedValue(TMessage message, int index) {
  76. throw new InvalidOperationException();
  77. }
  78. public void SetRepeated(TBuilder builder, int index, object value) {
  79. throw new InvalidOperationException();
  80. }
  81. public void AddRepeated(TBuilder builder, object value) {
  82. throw new InvalidOperationException();
  83. }
  84. public object GetRepeatedWrapper(TBuilder builder) {
  85. throw new InvalidOperationException();
  86. }
  87. #endregion
  88. }
  89. }