I find that I repeat a lot, and this, of course, is not good. So I wondered if I could do anything about it. This is the regular code in my WPF application:
private string _name; public string Name { get { return _name; } set { if (_name != value) { _name = value; OnPropertyChanged("Name"); } } }
So I was wondering if I could somehow wrap the setter to make it better and more readable. One idea was something like this:
protected void PropertySetter<T>(T property, T value, string name) { if (EqualityComparer<T>.Default.Equals(property, value)) { property = value; OnPropertyChanged(name); } }
Using:
private string _name2; public string Name2 { get { return _name2; } set { PropertySetter<string>(Name2, value, "Name2"); } }
But I'm not sure if this is really smart or will it work with value types?
I guess I'm not the first to try something like this, so if someone knows a good reliable way to do something like this, please call back. I suppose I could not make the Changed typeafe property without reflection, but any ideas there also help.
source share