Closed failwyn closed 1 year ago
This is by design, and it's just an optimization. You're seeing default
being passed because you're not implementing any of those partial methods that need oldValue
, so this way the generated code is avoiding one unnecessary field read. You can try implementing any of those methods and if you check the generated code you'll see it'll automatically change to pass the previous property value as expected. The docs show that code because the method is implemented there 🙂
@Sergio0694 sorry realised this has been closed, I was reviewing the generated code and was also confused by this. If you are recognising whether the partial method has implementation why not remove the call altogether? Not sure if this is more clear?
i.e. if partial method has implementation
public string Name
{
get => name;
[global::System.Diagnostics.CodeAnalysis.MemberNotNull("name")]
set
{
if (!global::System.Collections.Generic.EqualityComparer<string>.Default.Equals(name, value))
{
OnNameChanging(value);
OnNameChanging(name, value);
OnPropertyChanging(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangingArgs.Name);
name = value;
OnNameChanged(value);
OnNameChanged(name, value);
OnPropertyChanged(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedArgs.Name);
}
}
}
if no partial methods implemented
public string Name
{
get => name;
[global::System.Diagnostics.CodeAnalysis.MemberNotNull("name")]
set
{
if (!global::System.Collections.Generic.EqualityComparer<string>.Default.Equals(name, value))
{
OnPropertyChanging(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangingArgs.Name);
name = value;
OnPropertyChanged(global::CommunityToolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedArgs.Name);
}
}
}
I'm not thinking from a performance perspective. Rather making it obvious what partial methods actually get called with some possible side effect?
Describe the bug
In CommunityToolkit.Mvvm 8.2.0.0 the ObservableProperty Attribute SourceGenerator is generating code that sends default as the oldValue toOn{PropertyName}Changing(oldValue, newValue) and On{PropertyName}Changed(oldValue, newValue).
Steps to reproduce
The correct code is displayed in the documentation at https://learn.microsoft.com/en-us/dotnet/communitytoolkit/mvvm/generators/observableproperty#running-code-upon-changes
When compiling the following code
This code is generated
Expected behavior
The oldValue should be sent to OnNameChanging and OnNameChanged instead of the default value for the type.
IDE and version
VS 2022
IDE version
17.5.3
Nuget packages
Nuget package version(s)
8.2.0.0
Help us help you
No, just wanted to report this