Notify Attribute
The [Notify] attribute generates boilerplate for property change notification, so you donβt have to manually write INotifyPropertyChanged logic. It is designed for MVVM scenarios where you want clean view-models without repeating the same patterns.
β¨ What it does
Normally you would write:
public class Person : INotifyPropertyChanged { private string _name;
public string Name
{
get => _name;
set
{
if (_name == value) return;
_name = value;
OnPropertyChanged(nameof(Name));
}
}
public event PropertyChangedEventHandler? PropertyChanged;
protected void OnPropertyChanged(string propertyName)
=> PropertyChanged?.Invoke(this, new(propertyName));
}
public partial class Person
{
[Notify]
private string _name;
}
π― Benefits
No duplicated OnPropertyChanged code Strongly-typed, compiler-generated properties Easier refactoring (rename the field β property updates automatically) Less noise in your view-models
π Generated property
From the field:
[Notify] private string _name;
The generator produces something like:
public string Name {
get => _name;
set
{
if (_name == value) return;
_name = value;
OnPropertyChanged(nameof(Name));
}
}
π Notes
Works in any class marked partial Requires INotifyPropertyChanged support in your base type Generated code is placed in a .g.cs file β do not edit it
π How Notify Works
When you mark a backing field with [Notify], the generator:
- Ensures the type implements
INotifyPropertyChanged - Generates a public property for the field
- Raises 'OnPropertyChanged' when the value changes
- Skips notifications when the value hasnβt changed
- (Optional) generates strongly-typed change hooks
If hooks are enabled for the field, the following methods are available:
OnXChanging(oldValue, ref newValue, ref cancel)OnXChanged(oldValue, newValue)
Use these to:
- validate and cancel changes
- modify the incoming value (coercion)
- react to changes after assignment
The final property change event is raised only after the value is successfully updated.