SinglePrimitiveAccessor.cs 3.9 KB

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