Skip to content

Allow single parameter with any type when binding to method - #21867

Merged
MrJul merged 5 commits into
AvaloniaUI:mainfrom
MrJul:fix/binding-method-resolution
Jul 28, 2026
Merged

Allow single parameter with any type when binding to method#21867
MrJul merged 5 commits into
AvaloniaUI:mainfrom
MrJul:fix/binding-method-resolution

Conversation

@MrJul

@MrJul MrJul commented Jul 27, 2026

Copy link
Copy Markdown
Member

What does the pull request do?

#21617 made the chosen overload consistent when binding a command property to a method, instead of it being undefined behavior: the parameter must be an object.
While doing so, it also broke code that strongly typed the parameter with a single overload, see #21833.

This PR improves the logic to allow a single overload with a single parameter to always match (the parameter's type doesn't matter in this case), which should cover the most common scenarios, as view models don't typically overload such methods.

New logic

  • Overloads with more than one parameter are ignored.
  • If there's a single overload accepting one parameter, it is chosen.
  • If there's no overloads accepting one parameter but there's one with zero parameters, it is chosen.
  • If there's several overloads accepting one parameter and one is of type object, it is chosen.

Any other combination results in an error. For example multiple one-parameter overloads (without object) don't work. There is no overload resolution matching C#: we don't want to implement such a complex algorithm, and the exact parameter type might not be known at compile time.

These changes apply to both compiled and reflection bindings.

There is still one difference between the two binding types though: reflection bindings convert the parameter to the proper type at runtime, compiled bindings don't (the correct type must be passed, or an InvalidCastException is thrown). This is to avoid calling TryConvert, which isn't AOT-compatible. I'm not a fan of this difference and will be happy to further discuss it.

Fixed issues

@avaloniaui-bot

Copy link
Copy Markdown

You can test this PR using the following package version. 12.2.999-cibuild0067805-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

Comment thread tests/Avalonia.Markup.Xaml.UnitTests/Data/BindingTests_Method.cs
@MrJul
MrJul requested a review from maxkatz6 July 28, 2026 11:59
maxkatz6
maxkatz6 previously approved these changes Jul 28, 2026
@MrJul
MrJul force-pushed the fix/binding-method-resolution branch from a6e774e to 3fd9a40 Compare July 28, 2026 12:38
@MrJul
MrJul added this pull request to the merge queue Jul 28, 2026
@avaloniaui-bot

Copy link
Copy Markdown

You can test this PR using the following package version. 12.2.999-cibuild0067863-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

Merged via the queue into AvaloniaUI:main with commit 5495737 Jul 28, 2026
9 checks passed
@MrJul
MrJul deleted the fix/binding-method-resolution branch July 28, 2026 14:20
MrJul added a commit that referenced this pull request Jul 29, 2026
* Allow single parameter with any type when binding to method

* Port method binding logic to ReflectionBinding

* Don't depend on the order of methods

* Handle overrides properly

* Fix nullability warning
@MrJul MrJul added backported-12.1.x and removed backport-candidate-12.1.x Consider this PR for backporting to 12.1 branch labels Jul 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

v12.1.0 breaks compiled bindings / reduces type safety

3 participants