Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On-PropName-Changed not called for dependent properties #700

Closed
cell001nz opened this issue Feb 9, 2021 · 3 comments
Closed

On-PropName-Changed not called for dependent properties #700

cell001nz opened this issue Feb 9, 2021 · 3 comments
Labels
Milestone

Comments

@cell001nz
Copy link
Contributor

I've avoided upgrading since 3.1.3 due to a behaviour change in 3.2.0 which introduces bugs in my code. It would be good to know if the change was intentional.

With 3.1.3 the code below prints:
Length changed to 3
Name changed to One

If I upgrade to 3.2.0:
Name changed to One

This seems to be because >= 3.2.0 has stopped calling On-PropName-Changed for dependencies. It looks like a property changed event is raised for the dependent property, but not the associated On-Property-Changed method;

static void Main(string[] args)
{
   Thing thing = new Thing();

   thing.Name = "One";
}

[AddINotifyPropertyChangedInterface]
public class Thing
{
   public string Name { get; set; }

   public int Length => Name?.Length ?? 0;

   private void OnNameChanged() => Console.WriteLine($"Name changed to {Name}");
   private void OnLengthChanged() => Console.WriteLine($"Length changed to {Length}");
}
@ltrzesniewski
Copy link
Member

Hmm I don't think that was an intentional change...

The build produces the following warning:

Warning : Fody/PropertyChanged: Type ConsoleApp.Thing does not contain a Length property with an injected change notification, and therefore the OnLengthChanged method will not be called. You can suppress this warning with [SuppressPropertyChangedWarnings].

And here's the setter of Name:

[CompilerGenerated]
set
{
    if (string.Equals(this.<Name>k__BackingField, value, StringComparison.Ordinal))
    {
        return;
    }
    this.<Name>k__BackingField = value;
    this.<>OnPropertyChanged(<>PropertyChangedEventArgs.Length);
    this.OnNameChanged();
    this.<>OnPropertyChanged(<>PropertyChangedEventArgs.Name);
}

@cell001nz
Copy link
Contributor Author

So it sees there is no setter for Length, therefore no possible call to OnLengthChanged(), and incorrectly concludes that a call to OnLengthChanged() shouldn't be generated elsewhere, ie in the Name setter.

I see if I change the Length property to the following, it will work:

public int Length { get => Name?.Length ?? 0; set { } }

@cell001nz
Copy link
Contributor Author

Looks like it was the following revision which introduced the problem. I'm going to have a look and see if there is anything I can do, but I've not looked at the code before so there's no telling how it will go.

Revision: 65ea5fe
Date: 08/12/2019 22:23:19

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants