From f7b417ddfe63cb4d39775e5fd4560894cc547d65 Mon Sep 17 00:00:00 2001 From: Jie Luo Date: Fri, 29 May 2015 12:48:25 -0700 Subject: Add oneof support for C# --- .../FieldAccess/SinglePrimitiveAccessor.cs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) (limited to 'csharp/src/ProtocolBuffers/FieldAccess/SinglePrimitiveAccessor.cs') 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 setValueDelegate; private readonly Func hasDelegate; private readonly Func clearDelegate; + private readonly Func caseDelegate; /// /// 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(hasProperty.GetGetMethod()); } else { - hasDelegate = message => !GetValue(message).Equals(fieldDescriptor.DefaultValue); + if (fieldDescriptor.ContainingOneof != null) + { + PropertyInfo caseProperty = typeof(TMessage).GetProperty(containingOneofName + "Case"); + caseDelegate = ReflectionUtil.CreateUpcastDelegate(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(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 +} -- cgit v1.2.3