diff options
author | Jon Skeet <skeet@pobox.com> | 2015-06-09 19:30:44 +0100 |
---|---|---|
committer | Jon Skeet <skeet@pobox.com> | 2015-06-09 19:30:44 +0100 |
commit | e38294a62d7f37c0661273a9a26fda16d557423f (patch) | |
tree | 316989251907553408e7b32a12792f496333e075 /csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorBase.cs | |
parent | f52426827e4d5e8da7d205af538799740b5199b9 (diff) | |
download | protobuf-e38294a62d7f37c0661273a9a26fda16d557423f.tar.gz protobuf-e38294a62d7f37c0661273a9a26fda16d557423f.tar.bz2 protobuf-e38294a62d7f37c0661273a9a26fda16d557423f.zip |
First pass at the mutable API. Quite a bit more to do - in particular, it's pretty slow right now.
Diffstat (limited to 'csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorBase.cs')
-rw-r--r-- | csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorBase.cs | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorBase.cs b/csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorBase.cs new file mode 100644 index 00000000..51b94931 --- /dev/null +++ b/csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorBase.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using Google.Protobuf; +using Google.Protobuf.FieldAccess; + +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> + { + private readonly Func<T, object> getValueDelegate; + + internal FieldAccessorBase(string name) + { + PropertyInfo property = typeof(T).GetProperty(name); + if (property == null || !property.CanRead) + { + throw new ArgumentException("Not all required properties/methods available"); + } + getValueDelegate = ReflectionUtil.CreateUpcastDelegate<T>(property.GetGetMethod()); + } + + public object GetValue(T message) + { + return getValueDelegate(message); + } + + public abstract bool HasValue(T message); + public abstract void Clear(T message); + public abstract void SetValue(T message, object value); + } +} |