aboutsummaryrefslogtreecommitdiff
path: root/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs
diff options
context:
space:
mode:
authorJan Tattermusch <jtattermusch@users.noreply.github.com>2015-07-22 10:36:06 -0700
committerJan Tattermusch <jtattermusch@users.noreply.github.com>2015-07-22 10:36:06 -0700
commit7b5c3967991b6534f439cb31b0d247501f4a0ef8 (patch)
treee161b4bfaa308ca173ee9a8b2ff858c5b76b618b /csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs
parent2ee4b5665520fe3245eb5e15df8bd35e0c539a07 (diff)
parent43d64b4f54c8cf9521aded37fc695faba28793ee (diff)
downloadprotobuf-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/FieldDescriptor.cs')
-rw-r--r--csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs30
1 files changed, 29 insertions, 1 deletions
diff --git a/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs b/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs
index 3d9d0d75..a8609b8a 100644
--- a/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs
+++ b/csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs
@@ -31,6 +31,7 @@
#endregion
using System;
+using System.Linq;
namespace Google.Protobuf.Reflection
{
@@ -45,9 +46,11 @@ namespace Google.Protobuf.Reflection
private readonly MessageDescriptor containingType;
private readonly OneofDescriptor containingOneof;
private FieldType fieldType;
+ private readonly string propertyName; // Annoyingly, needed in Crosslink.
+ private IFieldAccessor accessor;
internal FieldDescriptor(FieldDescriptorProto proto, FileDescriptor file,
- MessageDescriptor parent, int index)
+ MessageDescriptor parent, int index, string propertyName)
: base(file, file.ComputeFullName(parent, proto.Name), index)
{
this.proto = proto;
@@ -74,6 +77,12 @@ namespace Google.Protobuf.Reflection
}
file.DescriptorPool.AddSymbol(this);
+ // We can't create the accessor until we've cross-linked, unfortunately, as we
+ // may not know whether the type of the field is a map or not. Remember the property name
+ // for later.
+ // We could trust the generated code and check whether the type of the property is
+ // a MapField, but that feels a tad nasty.
+ this.propertyName = propertyName;
}
/// <summary>
@@ -82,6 +91,8 @@ namespace Google.Protobuf.Reflection
public override string Name { get { return proto.Name; } }
internal FieldDescriptorProto Proto { get { return proto; } }
+
+ public IFieldAccessor Accessor { get { return accessor; } }
/// <summary>
/// Maps a field type as included in the .proto file to a FieldType.
@@ -287,6 +298,23 @@ namespace Google.Protobuf.Reflection
{
throw new DescriptorValidationException(this, "MessageSet format is not supported.");
}
+ accessor = CreateAccessor(propertyName);
+ }
+
+ private IFieldAccessor CreateAccessor(string propertyName)
+ {
+ if (containingType.GeneratedType == null || propertyName == null)
+ {
+ return null;
+ }
+ var property = containingType.GeneratedType.GetProperty(propertyName);
+ if (property == null)
+ {
+ throw new DescriptorValidationException(this, "Property " + propertyName + " not found in " + containingType.GeneratedType);
+ }
+ return IsMap ? new MapFieldAccessor(property, this)
+ : IsRepeated ? new RepeatedFieldAccessor(property, this)
+ : (IFieldAccessor) new SingleFieldAccessor(property, this);
}
}
} \ No newline at end of file