Error MVVMTK0015 when the desired NotifyPropertyChangedFor property is an explicit interface implementation
Describe the bug
A compilation error results for a situation that we think should be valid. Specifically:
error MVVMTK0015: The target(s) of [NotifyPropertyChangedFor] must be a (different) accessible property, but "(the notify-for property name)" has no (other) matches in type (my type) (https://aka.ms/mvvmtoolkit/errors/mvvmtk0015)
It appears that the toolkit's conditions for NotifiyPropertyChangedFor are not satisfied when the property exists only as an explicitly-implemented interface property.
Regression
No response
Steps to reproduce
Given this simplified reproduction:
public interface IMenuItem
{
bool Enabled { get; }
}
public partial class MenuItem : ObservableObject, IMenuItem
{
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(IMenuItem.Enabled))]
public partial int ValueThatAffectsEnabled { get; set; }
bool IMenuItem.Enabled => ValueThatAffectsEnabled > 0;
}
Compiling results in the following error:
error MVVMTK0015: The target(s) of [NotifyPropertyChangedFor] must be a (different) accessible property, but "Enabled" has no (other) matches in type MenuItem (https://aka.ms/mvvmtoolkit/errors/mvvmtk0015)
Expected behavior
We expect this to be allowed by the NotifyPropertyChangedFor attribute's validation because:
- There is a public property named
Enabled, it's simply of lessened visibility (only visible through the interface) - It is a very common for our views to work with their View Models as interfaces, not concrete types. This lets us swap in/out compatible view model types.
We can work around this:
- We could make those properties not explicit interface implementations (we'd rather not, we design member visibility very intentionally).
- We could add OnXXXXChanged() partial methods to manually call
OnPropertyChanged("the desired property").
but it would be better if the library would allow this.
Screenshots
No response
IDE and version
VS 2022
IDE version
17.14.10
Nuget packages
- [ ] CommunityToolkit.Common
- [ ] CommunityToolkit.Diagnostics
- [ ] CommunityToolkit.HighPerformance
- [x] CommunityToolkit.Mvvm (aka MVVM Toolkit)
Nuget package version(s)
8.4.0
Additional context
No response
Help us help you
No, just wanted to report this
I think the problem seems to be here
https://github.com/CommunityToolkit/dotnet/blob/657c6971a8d42655c648336b781639ed96c2c49f/src/CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservablePropertyGenerator.Execute.cs#L478
https://github.com/CommunityToolkit/dotnet/blob/657c6971a8d42655c648336b781639ed96c2c49f/src/CommunityToolkit.Mvvm.SourceGenerators/Extensions/INamedTypeSymbolExtensions.cs#L59
This extension method only finds all the members of the class in the hierarchy of classes, not the implemented interfaces. The implicit members belong to the class and that way it works. An explicit implementation I think doesn't belong to the hierarchy.
The solution (if possible, I don't know if I am not catching something) should be something like this. This solution has the problem that the implicit members will be returned twice (and I don't know if Distinct-SymbolEqualityComparer will work)
public static IEnumerable<ISymbol> GetAllMembers(this INamedTypeSymbol symbol, string name)
{
foreach (var iface in symbol.AllInterfaces)
{
foreach (var member in iface.GetMembers(name))
yield return member;
}
for (INamedTypeSymbol? current = symbol;
current is { SpecialType: not SpecialType.System_Object };
current = current.BaseType)
{
foreach (var member in current.GetMembers(name))
yield return member;
}
}