I want to write a generic SetProperty method for a MVVM ViewModel which right now looks like this:
protected virtual bool SetProperty<T>(ref T storage, T value, string name)
{
if (EqualityComparer<T>.Default.Equals(storage, value))
return false;
storage = value;
OnPropertyChanged(name);
return true;
}
The problem is that this would compare float-point values (float, double, decimal) with Equals instead of a Math.Abs(storage - value) < EPSILON
as you should compare float-point values. The easy approach would be to check the two values whether they're float and then compare them. The same with double and decimal. Is there an easier way I compare two generic types whether they're float-point and compare them accordingly?
(I couldn't find any documentation if EqualityComparer<T>.Default
already considers this)