CommunityToolkit / dotnet

.NET Community Toolkit is a collection of helpers and APIs that work for all .NET developers and are agnostic of any specific UI platform. The toolkit is maintained and published by Microsoft, and part of the .NET Foundation.
https://docs.microsoft.com/dotnet/communitytoolkit/?WT.mc_id=dotnet-0000-bramin
Other
3.07k stars 299 forks source link

OnChanged and OnChanged not accessible on inherit classes #629

Closed CesGan closed 1 year ago

CesGan commented 1 year ago

Describe the bug

Methods OnChanged and OnChanging are not accessible on inherited classes.

public class Class1 : ViewModelBase
{
    public Class1()
    {
        Name = "Foo";
    }

    partial void OnNameChanged(string value) // -> CS0759   No defining declaration found for implementing declaration of partial method 'Class1.OnNameChanged(string)'

    {
        throw new NotImplementedException();
    }

    partial void OnNameChanging(string value) // -> CS0759  No defining declaration found for implementing declaration of partial method 'Class1.OnNameChanging(string)'

    {
        throw new NotImplementedException();
    }
}

public abstract partial class ViewModelBase : ObservableObject
{
    [ObservableProperty]
    private string name;

    partial void OnNameChanged(string value) // -> Works
    {
        throw new NotImplementedException();
    }

    partial void OnNameChanging(string value) // -> Works
    {
        throw new NotImplementedException();
    }
}

Expected behavior

Methods should be available.

IDE and version

VS 2022

Nuget packages

Nuget package version(s)

8.1.0

Help us help you

No, just wanted to report this

Sergio0694 commented 1 year ago

This is by design, partial methods with no accessibility modifier are effectively private. These can't be made protected because that would force you to always implement them, otherwise the code would not compile. If you absolutely need derived class to be able to inject logic, you can add a protected virtual method for that, and call it from those two private methods.