I was looking at Implementing INotifyPropertyChanged - does a better way exist?, (specifically the accepted answer) and this works great for any cases where you don't need or aren't using a base class. However, I would like to implement it using another interface, taking advantage of explicit implementation (https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/interfaces/explicit-interface-implementation) in order to allow implementing classes to inherit from other base classes without modifying the said base class.
I tried doing something along the lines of:
public interface INotifyPropertyChangedWithHelper : INotifyPropertyChanged
{
void SetProperty<T>(T value, ref T property, string propertyName)
{
if (!EqualityComparer<T>.Default.Equals(property, value))
{
property = value;
OnPropertyChanged(propertyName);
}
}
void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
but that last line throws an error CS0079 (The event 'INotifyPropertyChanged.PropertyChanged
' can only appear on the left hand side of +=
or -=
). I am assuming this because PropertyChanged
can't be invoked from anywhere besides the class that implements it, but is there any way to work around this?
If the answer is "no, you have to use a base class", that's fine, but I'd love to make this work in a way that doesn't block off the use of other base classes.