Skip to content

Commit aa79478

Browse files
Copilotjaviercn
andcommitted
Address review feedback: Use nameof(PersistentStateAttribute) and simplify property lookup
Co-authored-by: javiercn <[email protected]>
1 parent 6788165 commit aa79478

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

src/Components/Components/src/PersistentState/PersistentValueProviderComponentSubscription.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -231,30 +231,28 @@ private static PropertyGetter PropertyGetterFactory((Type type, string propertyN
231231
var propertyInfo = GetPropertyInfo(type, propertyName);
232232
if (propertyInfo == null)
233233
{
234+
// Check if the property exists but is not public to provide a better error message
235+
var nonPublicProperty = type.GetProperty(propertyName, ComponentProperties.BindablePropertyFlags);
236+
if (nonPublicProperty != null)
237+
{
238+
throw new InvalidOperationException(
239+
$"The property '{propertyName}' on component type '{type.FullName}' cannot be used with {nameof(PersistentStateAttribute)} because it is not public. Properties with {nameof(PersistentStateAttribute)} must have a public getter.");
240+
}
241+
234242
throw new InvalidOperationException($"Property {propertyName} not found on type {type.FullName}");
235243
}
236244

237245
// Check if the property is public
238246
if (propertyInfo.GetMethod == null || !propertyInfo.GetMethod.IsPublic)
239247
{
240248
throw new InvalidOperationException(
241-
$"The property '{propertyName}' on component type '{type.FullName}' cannot be used with PersistentState because it is not public. Properties with PersistentState must be public.");
249+
$"The property '{propertyName}' on component type '{type.FullName}' cannot be used with {nameof(PersistentStateAttribute)} because it is not public. Properties with {nameof(PersistentStateAttribute)} must have a public getter.");
242250
}
243251

244252
return new PropertyGetter(type, propertyInfo);
245253

246254
static PropertyInfo? GetPropertyInfo([DynamicallyAccessedMembers(LinkerFlags.Component)] Type type, string propertyName)
247-
{
248-
// First try to find the property with public access only (for performance in the common case)
249-
var publicProperty = type.GetProperty(propertyName);
250-
if (publicProperty != null)
251-
{
252-
return publicProperty;
253-
}
254-
255-
// If not found as public, try with all flags to see if it exists as private/protected
256-
return type.GetProperty(propertyName, ComponentProperties.BindablePropertyFlags);
257-
}
255+
=> type.GetProperty(propertyName);
258256
}
259257

260258
private static partial class Log

0 commit comments

Comments
 (0)