diff options
author | Jon Skeet <jonskeet@google.com> | 2015-07-01 14:47:03 +0100 |
---|---|---|
committer | Jon Skeet <jonskeet@google.com> | 2015-07-09 08:24:49 +0100 |
commit | 78ea98f56f7a0a028e378aee6394549707e199bc (patch) | |
tree | b3275e8d8b01528dc58af6ec0877c4ee79cd1b54 /csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorBase.cs | |
parent | 3805b43009d35422bed85c56aaf2d6ce4d007fe5 (diff) | |
download | protobuf-78ea98f56f7a0a028e378aee6394549707e199bc.tar.gz protobuf-78ea98f56f7a0a028e378aee6394549707e199bc.tar.bz2 protobuf-78ea98f56f7a0a028e378aee6394549707e199bc.zip |
Implement reflection properly for fields.
- FieldAccessorTable is now non-generic
- We don't have a static field per message type in the umbrella class. (Message descriptors are accessed via the file descriptor.)
- Removed the "descriptor assigner" complication from the descriptor fixup; without extensions, we don't need it
- MapField implements IDictionary (more tests would be good...)
- RepeatedField implements IList (more tests would be good)
- Use expression trees to build accessors. (Will need to test this on various platforms... probably need a fallback strategy just using reflection directly.)
- Added FieldDescriptor.IsMap
- Added tests for reflection with generated messages
Changes to generated code coming in next commit.
Diffstat (limited to 'csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorBase.cs')
-rw-r--r-- | csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorBase.cs | 23 |
1 files changed, 13 insertions, 10 deletions
diff --git a/csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorBase.cs b/csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorBase.cs index 73d777b2..2a3e5b8b 100644 --- a/csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorBase.cs +++ b/csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorBase.cs @@ -32,34 +32,37 @@ using System; using System.Reflection; +using Google.Protobuf.Descriptors; namespace Google.Protobuf.FieldAccess { /// <summary> /// Base class for field accessors. /// </summary> - /// <typeparam name="T">Type of message containing the field</typeparam> - internal abstract class FieldAccessorBase<T> : IFieldAccessor<T> where T : IMessage<T> + internal abstract class FieldAccessorBase : IFieldAccessor { - private readonly Func<T, object> getValueDelegate; + private readonly Func<object, object> getValueDelegate; + private readonly FieldDescriptor descriptor; - internal FieldAccessorBase(string name) + internal FieldAccessorBase(Type type, string propertyName, FieldDescriptor descriptor) { - PropertyInfo property = typeof(T).GetProperty(name); + PropertyInfo property = type.GetProperty(propertyName); if (property == null || !property.CanRead) { throw new ArgumentException("Not all required properties/methods available"); } - getValueDelegate = ReflectionUtil.CreateUpcastDelegate<T>(property.GetGetMethod()); + this.descriptor = descriptor; + getValueDelegate = ReflectionUtil.CreateFuncObjectObject(property.GetGetMethod()); } - public object GetValue(T message) + public FieldDescriptor Descriptor { get { return descriptor; } } + + public object GetValue(object message) { return getValueDelegate(message); } - public abstract bool HasValue(T message); - public abstract void Clear(T message); - public abstract void SetValue(T message, object value); + public abstract void Clear(object message); + public abstract void SetValue(object message, object value); } } |