aboutsummaryrefslogtreecommitdiff
path: root/csharp/src/ProtocolBuffers/FieldAccess/SinglePrimitiveAccessor.cs
diff options
context:
space:
mode:
authorJie Luo <jieluo@google.com>2015-05-29 12:48:25 -0700
committerJie Luo <jieluo@google.com>2015-05-29 13:29:30 -0700
commitf7b417ddfe63cb4d39775e5fd4560894cc547d65 (patch)
tree2ffb9956310e6aeb46078e7ad58db94d517f1d86 /csharp/src/ProtocolBuffers/FieldAccess/SinglePrimitiveAccessor.cs
parent55df12194f5f0348c3ddf0636c63ba2b3978fab5 (diff)
downloadprotobuf-f7b417ddfe63cb4d39775e5fd4560894cc547d65.tar.gz
protobuf-f7b417ddfe63cb4d39775e5fd4560894cc547d65.tar.bz2
protobuf-f7b417ddfe63cb4d39775e5fd4560894cc547d65.zip
Add oneof support for C#
Diffstat (limited to 'csharp/src/ProtocolBuffers/FieldAccess/SinglePrimitiveAccessor.cs')
-rw-r--r--csharp/src/ProtocolBuffers/FieldAccess/SinglePrimitiveAccessor.cs22
1 files changed, 19 insertions, 3 deletions
diff --git a/csharp/src/ProtocolBuffers/FieldAccess/SinglePrimitiveAccessor.cs b/csharp/src/ProtocolBuffers/FieldAccess/SinglePrimitiveAccessor.cs
index b9ab7293..035fcf3c 100644
--- a/csharp/src/ProtocolBuffers/FieldAccess/SinglePrimitiveAccessor.cs
+++ b/csharp/src/ProtocolBuffers/FieldAccess/SinglePrimitiveAccessor.cs
@@ -47,6 +47,7 @@ namespace Google.ProtocolBuffers.FieldAccess
private readonly Action<TBuilder, object> setValueDelegate;
private readonly Func<TMessage, bool> hasDelegate;
private readonly Func<TBuilder, IBuilder> clearDelegate;
+ private readonly Func<TMessage, object> caseDelegate;
/// <summary>
/// The CLR type of the field (int, the enum type, ByteString, the message etc).
@@ -57,7 +58,8 @@ namespace Google.ProtocolBuffers.FieldAccess
get { return clrType; }
}
- internal SinglePrimitiveAccessor(FieldDescriptor fieldDescriptor, string name, bool supportFieldPresence)
+ internal SinglePrimitiveAccessor(
+ FieldDescriptor fieldDescriptor, string name, string containingOneofName, bool supportFieldPresence)
{
PropertyInfo messageProperty = typeof(TMessage).GetProperty(name, null, ReflectionUtil.EmptyTypes);
PropertyInfo builderProperty = typeof(TBuilder).GetProperty(name, null, ReflectionUtil.EmptyTypes);
@@ -77,7 +79,16 @@ namespace Google.ProtocolBuffers.FieldAccess
hasDelegate = ReflectionUtil.CreateDelegateFunc<TMessage, bool>(hasProperty.GetGetMethod());
} else
{
- hasDelegate = message => !GetValue(message).Equals(fieldDescriptor.DefaultValue);
+ if (fieldDescriptor.ContainingOneof != null)
+ {
+ PropertyInfo caseProperty = typeof(TMessage).GetProperty(containingOneofName + "Case");
+ caseDelegate = ReflectionUtil.CreateUpcastDelegate<TMessage>(caseProperty.GetGetMethod());
+ hasDelegate = message => OneofFieldNumber(message).Equals(fieldDescriptor.FieldNumber);
+ }
+ else
+ {
+ hasDelegate = message => !GetValue(message).Equals(fieldDescriptor.DefaultValue);
+ }
}
clrType = messageProperty.PropertyType;
@@ -86,6 +97,11 @@ namespace Google.ProtocolBuffers.FieldAccess
setValueDelegate = ReflectionUtil.CreateDowncastDelegate<TBuilder>(builderProperty.GetSetMethod());
}
+ private int OneofFieldNumber(TMessage message)
+ {
+ return (int) caseDelegate(message);
+ }
+
public bool Has(TMessage message)
{
return hasDelegate(message);
@@ -143,4 +159,4 @@ namespace Google.ProtocolBuffers.FieldAccess
#endregion
}
-} \ No newline at end of file
+}