Fix x:Bind interface property access on concrete types without public members#22325
Fix x:Bind interface property access on concrete types without public members#22325
Conversation
Modified GetPropertyInfo and GetIndexerInfo to check interface properties when not found in type hierarchy. This fixes x:Bind bindings that access properties defined on interfaces (e.g., IReadOnlyList.Count) when the concrete type is an array or has explicit interface implementations. Co-authored-by: MartinZikmund <1075116+MartinZikmund@users.noreply.github.com>
|
|
Co-authored-by: MartinZikmund <1075116+MartinZikmund@users.noreply.github.com>
- Add periods to end of comments for consistency - Extract bindingFlagsForInterfaces variable to reduce duplication Co-authored-by: MartinZikmund <1075116+MartinZikmund@users.noreply.github.com>
| @@ -0,0 +1,22 @@ | |||
| <!-- Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License. See LICENSE in the project root for license information. --> | |||
|
🤖 Your WebAssembly Skia Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-22325/wasm-skia-net9/index.html |
|
🤖 Your Docs stage site is ready! Visit it here: https://unodocsprstaging.z13.web.core.windows.net/pr-22325/docs/index.html |
There was a problem hiding this comment.
originalType.GetInterfaces() also return Type[]s
we could wrap the whole thing in a single foreach loop instead of duplicating the code:
IEnumerable<Type> EnumerateAllTypesAndInterfaces(Type type)
{
for (var t = type; t is { }; t = t.BaseType)
{
yield return t;
}
foreach (var t in type.GetInterfaces())
{
yield return t;
}
}
XyzInfo? GetXyzInfo(Type type, string name, bool allowPrivateMembers)
{
var flags =
BindingFlags.Public |
(allowPrivateMembers ? BindingFlags.NonPublic : BindingFlags.Default) |
BindingFlags.Instance | BindingFlags.Static;
foreach (var t in EnumerateAllTypesAndInterfaces())
{
var info = t.GetXyz(...,
flags | (!info.IsInterface ? BindingFlags.DeclaredOnly : BindingFlags.Default), ...
)
...
}
}|
This PR is stale because it has been open 60 days with no activity. Remove stale label or comment or it will be closed in 10 days. |
GitHub Issue: closes #
PR Type:
What is the current behavior? 🤔
x:Bind fails when accessing interface properties on concrete types that don't expose those properties publicly. For example:
Fails with:
Arrays implement
IReadOnlyList<T>.Countbut only exposeLengthdirectly. x:Bind uses reflection on the concrete type rather than checking interface implementations.What is the new behavior? 🚀
BindingPropertyHelpernow checks implemented interfaces when a property is not found in the type hierarchy:Core changes:
GetPropertyInfo: After checking class hierarchy, iterates throughGetInterfaces()to find matching propertiesGetIndexerInfo: Same pattern for interface indexers (e.g.,IReadOnlyList<T>[index])Trimming support:
DynamicallyAccessedMemberTypes.InterfacesannotationUnconditionalSuppressMessageforGetInterfaces()callsTests added:
BindingPropertyHelpercovering arrays, lists, indexers, and priority orderingPR Checklist ✅
Please check if your PR fulfills the following requirements:
Screenshots Compare Test Runresults.Other information ℹ️
Matches WinUI3/WinAppSDK behavior where x:Bind correctly resolves interface properties regardless of concrete type implementation.
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.