I'm creating an application in C # and I'm using the INotifyPropertyChanged
interface like this:
public class Test: INotifyPropertyChanged
{
private int foo;
public int Foo { get => foo; set => Set(ref foo, value); }
public void Set<T>(ref T property, T value, [CallerMemberName]string p = "")
{
property = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(p));
}
}
My problem is that whenever I want to implement this form of work (with the Set(ref property, value)
method I am required to come here and copy the code and reimplement it. From this problem, I thought of a method of extension like this:
using System.Runtime.CompilerServices;
public static class Extensions
{
public static void Set<T>(
this INotifyPropertyChanged source, ref T property, T value,
[CallerMemberName]string propertyName = "")
{
property = value;
source.PropertyChanged?
.Invoke(source, new PropertyChangedEventArgs(propertyName));
}
}
My problem is that if I try to do this implementation, I get this message:
The event
INotifyPropertyChanged.PropertyChanged
can only appear on the left hand side of + = or - =
What do I need to do to force the call of this event through an extension property?