SinglePrimitiveAccessor.cs 3.5 KB

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