Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[C#] Make FieldDescriptor propertyName public #7642

Merged
merged 1 commit into from Oct 14, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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