diff options
author | Jan Tattermusch <jtattermusch@users.noreply.github.com> | 2015-07-22 10:36:06 -0700 |
---|---|---|
committer | Jan Tattermusch <jtattermusch@users.noreply.github.com> | 2015-07-22 10:36:06 -0700 |
commit | 7b5c3967991b6534f439cb31b0d247501f4a0ef8 (patch) | |
tree | e161b4bfaa308ca173ee9a8b2ff858c5b76b618b /csharp/src/Google.Protobuf/Reflection/OneofDescriptor.cs | |
parent | 2ee4b5665520fe3245eb5e15df8bd35e0c539a07 (diff) | |
parent | 43d64b4f54c8cf9521aded37fc695faba28793ee (diff) | |
download | protobuf-7b5c3967991b6534f439cb31b0d247501f4a0ef8.tar.gz protobuf-7b5c3967991b6534f439cb31b0d247501f4a0ef8.tar.bz2 protobuf-7b5c3967991b6534f439cb31b0d247501f4a0ef8.zip |
Merge pull request #625 from jskeet/reflection-refactor
Reflection refactor
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); + } } } |