diff options
Diffstat (limited to 'csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorTable.cs')
-rw-r--r-- | csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorTable.cs | 29 |
1 files changed, 26 insertions, 3 deletions
diff --git a/csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorTable.cs b/csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorTable.cs index 57ea9c87..c69612fb 100644 --- a/csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorTable.cs +++ b/csharp/src/ProtocolBuffers/FieldAccess/FieldAccessorTable.cs @@ -42,6 +42,7 @@ namespace Google.Protobuf.FieldAccess public sealed class FieldAccessorTable
{
private readonly ReadOnlyCollection<IFieldAccessor> accessors;
+ private readonly ReadOnlyCollection<OneofAccessor> oneofs;
private readonly MessageDescriptor descriptor;
/// <summary>
@@ -51,7 +52,7 @@ namespace Google.Protobuf.FieldAccess /// <param name="type">The CLR type for the message.</param>
/// <param name="descriptor">The type's descriptor</param>
/// <param name="propertyNames">The Pascal-case names of all the field-based properties in the message.</param>
- public FieldAccessorTable(Type type, MessageDescriptor descriptor, string[] propertyNames)
+ public FieldAccessorTable(Type type, MessageDescriptor descriptor, string[] propertyNames, string[] oneofPropertyNames)
{
this.descriptor = descriptor;
var accessorsArray = new IFieldAccessor[descriptor.Fields.Count];
@@ -65,7 +66,13 @@ namespace Google.Protobuf.FieldAccess : (IFieldAccessor) new SingleFieldAccessor(type, name, field);
}
accessors = new ReadOnlyCollection<IFieldAccessor>(accessorsArray);
- // TODO(jonskeet): Oneof support
+ var oneofsArray = new OneofAccessor[descriptor.Oneofs.Count];
+ for (int i = 0; i < oneofsArray.Length; i++)
+ {
+ var oneof = descriptor.Oneofs[i];
+ oneofsArray[i] = new OneofAccessor(type, oneofPropertyNames[i], oneof);
+ }
+ oneofs = new ReadOnlyCollection<OneofAccessor>(oneofsArray);
}
// TODO: Validate the name here... should possibly make this type a more "general reflection access" type,
@@ -75,6 +82,10 @@ namespace Google.Protobuf.FieldAccess /// </summary>
public ReadOnlyCollection<IFieldAccessor> Accessors { get { return accessors; } }
+ public ReadOnlyCollection<OneofAccessor> Oneofs { get { return oneofs; } }
+
+ // TODO: Review the API for the indexers. Now that we have fields and oneofs, it's not as clear...
+
public IFieldAccessor this[int fieldNumber]
{
get
@@ -84,7 +95,7 @@ namespace Google.Protobuf.FieldAccess }
}
- internal IFieldAccessor this[FieldDescriptor field]
+ public IFieldAccessor this[FieldDescriptor field]
{
get
{
@@ -95,5 +106,17 @@ namespace Google.Protobuf.FieldAccess return accessors[field.Index];
}
}
+
+ public OneofAccessor this[OneofDescriptor oneof]
+ {
+ get
+ {
+ if (oneof.ContainingType != descriptor)
+ {
+ throw new ArgumentException("OneofDescriptor does not match message type.");
+ }
+ return oneofs[oneof.Index];
+ }
+ }
}
}
\ No newline at end of file |