diff options
Diffstat (limited to 'csharp/src/Google.Protobuf/Reflection/OneofDescriptor.cs')
-rw-r--r-- | csharp/src/Google.Protobuf/Reflection/OneofDescriptor.cs | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/csharp/src/Google.Protobuf/Reflection/OneofDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/OneofDescriptor.cs index e92dc8bb..a79d9de4 100644 --- a/csharp/src/Google.Protobuf/Reflection/OneofDescriptor.cs +++ b/csharp/src/Google.Protobuf/Reflection/OneofDescriptor.cs @@ -32,6 +32,7 @@ using System.Collections.Generic; using System.Collections.ObjectModel; +using System.Linq; namespace Google.Protobuf.Reflection { @@ -40,14 +41,16 @@ namespace Google.Protobuf.Reflection private readonly OneofDescriptorProto proto; private MessageDescriptor containingType; private IList<FieldDescriptor> fields; + private readonly OneofAccessor accessor; - internal OneofDescriptor(OneofDescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int index) + internal OneofDescriptor(OneofDescriptorProto proto, FileDescriptor file, MessageDescriptor parent, int index, string clrName) : base(file, file.ComputeFullName(parent, proto.Name), index) { this.proto = proto; containingType = parent; file.DescriptorPool.AddSymbol(this); + accessor = CreateAccessor(clrName); } /// <summary> @@ -62,6 +65,8 @@ namespace Google.Protobuf.Reflection public IList<FieldDescriptor> Fields { get { return fields; } } + public OneofAccessor Accessor { get { return accessor; } } + internal void CrossLink() { List<FieldDescriptor> fieldCollection = new List<FieldDescriptor>(); @@ -74,5 +79,25 @@ namespace Google.Protobuf.Reflection } fields = new ReadOnlyCollection<FieldDescriptor>(fieldCollection); } + + private OneofAccessor CreateAccessor(string clrName) + { + if (containingType.GeneratedType == null || clrName == null) + { + return null; + } + var caseProperty = containingType.GeneratedType.GetProperty(clrName + "Case"); + if (caseProperty == null) + { + throw new DescriptorValidationException(this, "Property " + clrName + "Case not found in " + containingType.GeneratedType); + } + var clearMethod = containingType.GeneratedType.GetMethod("Clear" + clrName, ReflectionUtil.EmptyTypes); + if (clearMethod == null) + { + throw new DescriptorValidationException(this, "Method Clear" + clrName + " not found in " + containingType.GeneratedType); + } + + return new OneofAccessor(caseProperty, clearMethod, this); + } } } |