The answer to this question has been edited to say that in C# 6.0, INotifyPropertyChanged can be implemented with the following OnPropertyChanged procedure:
I know this question is old, but here is my implementation
Bindable uses a dictionary as a property store. It's easy enough to add the necessary overloads for a subclass to manage its own backing field using ref parameters.
The code:
public class Bindable : INotifyPropertyChanged
{
private Dictionary _properties = new Dictionary();
///
/// Gets the value of a property
///
///
///
protected T Get([CallerMemberName] string name = null)
{
object value = null;
if (_properties.TryGetValue(name, out value))
return value == null ? default(T) : (T)value;
return default(T);
}
///
/// Sets the value of a property
///
///
///
///
protected void Set(T value, [CallerMemberName] string name = null)
{
if (Equals(value, Get(name)))
return;
_properties[name] = value;
OnPropertyChanged(name);
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
used like this
public class Item : Bindable
{
public Guid Id { get { return Get(); } set { Set(value); } }
}