Skip to content

Commit

Permalink
Merge pull request #7642 from philjdf/master
Browse files Browse the repository at this point in the history
[C#] Make FieldDescriptor propertyName public
  • Loading branch information
deannagarcia committed Oct 14, 2021
2 parents 5106dea + 115af28 commit 8b69867
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions csharp/src/Google.Protobuf/Reflection/FieldDescriptor.cs
Expand Up @@ -45,7 +45,6 @@ public sealed class FieldDescriptor : DescriptorBase, IComparable<FieldDescripto
private MessageDescriptor extendeeType;
private MessageDescriptor messageType;
private FieldType fieldType;
private readonly string propertyName; // Annoyingly, needed in Crosslink.
private IFieldAccessor accessor;

/// <summary>
Expand All @@ -70,6 +69,11 @@ public sealed class FieldDescriptor : DescriptorBase, IComparable<FieldDescripto
/// </summary>
public string JsonName { get; }

/// <summary>
/// The name of the property in the <c>ContainingType.ClrType</c> class.
/// </summary>
public string PropertyName { get; }

/// <summary>
/// Indicates whether this field supports presence, either implicitly (e.g. due to it being a message
/// type field) or explicitly via Has/Clear members. If this returns true, it is safe to call
Expand Down Expand Up @@ -123,7 +127,7 @@ public sealed class FieldDescriptor : DescriptorBase, IComparable<FieldDescripto
// 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;
PropertyName = propertyName;
Extension = extension;
JsonName = Proto.JsonName == "" ? JsonFormatter.ToJsonName(Proto.Name) : Proto.JsonName;
}
Expand Down Expand Up @@ -436,15 +440,15 @@ private IFieldAccessor CreateAccessor()
// If we're given no property name, that's because we really don't want an accessor.
// This could be because it's a map message, or it could be that we're loading a FileDescriptor dynamically.
// TODO: Support dynamic messages.
if (propertyName == null)
if (PropertyName == null)
{
return null;
}

var property = ContainingType.ClrType.GetProperty(propertyName);
var property = ContainingType.ClrType.GetProperty(PropertyName);
if (property == null)
{
throw new DescriptorValidationException(this, $"Property {propertyName} not found in {ContainingType.ClrType}");
throw new DescriptorValidationException(this, $"Property {PropertyName} not found in {ContainingType.ClrType}");
}
return IsMap ? new MapFieldAccessor(property, this)
: IsRepeated ? new RepeatedFieldAccessor(property, this)
Expand Down

0 comments on commit 8b69867

Please sign in to comment.