I've written the following extension method:
public static void NotifyChanged<T>(this INotifyPropertyChanged inpc, ref T current, T newValue, Action<PropertyChangedEventArgs> eventRaiser, [CallerMemberName] string? name = null) where T : IEquatable<T> { if (current.Equals(newValue)) { return; } current = newValue; eventRaiser(new PropertyChangedEventArgs(name)); } that can be used like this:
public class Foo : Bar, INotifyPropertyChanged { public event PropertyChangedEventHandler? PropertyChanged; private string? rootExpression; public string? RootExpression { get => rootExpression; set => this.NotifyChanged(ref rootExpression, value, args => PropertyChanged?.Invoke(this, args)); } } This saves much of the boilerplate of writing INPC-aware properties.
However, I now get a compiler warning error at the call to NotifyChanged:
The type 'string?' cannot be used as type parameter 'T' in the generic type or method 'INotifyPropertyChangedExtensions.NotifyChanged(INotifyPropertyChanged, ref T, T, Action, string?)'. Nullability of type argument 'string?' doesn't match constraint type 'System.IEquatable'.
AFAICT the error is saying that string? cannot be cast to IEquatable<string?>, only string can be cast to IEquatable<string>.
How can I resolve this? Apply some attribute? Or something else?