Skip to content

Commit dbf196d

Browse files
committed
Fixed bug with reflection in TryGetContentControl
1 parent bf823e0 commit dbf196d

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

Microsoft.Toolkit.Uwp.UI/Extensions/Tree/LogicalTree.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -566,13 +566,24 @@ public static IEnumerable<FrameworkElement> FindParents(this FrameworkElement el
566566
/// <returns>The retrieved content control, or <see langword="null"/> if not available.</returns>
567567
public static UIElement? TryGetContentControl(this FrameworkElement element)
568568
{
569-
Type type = element.GetType();
569+
Type? type = element.GetType();
570570

571-
if (type.GetCustomAttribute<ContentPropertyAttribute>(true) is ContentPropertyAttribute attribute &&
572-
type.GetProperty(attribute.Name) is PropertyInfo propertyInfo &&
573-
propertyInfo.GetValue(element) is UIElement content)
571+
while (type is not null)
574572
{
575-
return content;
573+
// We need to manually explore the custom attributes this way as the target one
574+
// one is not returned by any of the other available GetCustomAttribute<T> APIs.
575+
foreach (CustomAttributeData attribute in type.CustomAttributes)
576+
{
577+
if (attribute.AttributeType == typeof(ContentPropertyAttribute))
578+
{
579+
string propertyName = (string)attribute.NamedArguments[0].TypedValue.Value;
580+
PropertyInfo propertyInfo = type.GetProperty(propertyName);
581+
582+
return propertyInfo.GetValue(element) as UIElement;
583+
}
584+
}
585+
586+
type = type.BaseType;
576587
}
577588

578589
return null;

0 commit comments

Comments
 (0)