Let's say that I have the following class:
public class TsvDataModel : IEquatable<TsvDataModel>
{
public int ElementId { get; set; }
public string Location { get; set; }
public string RoomKey { get; set;
public decimal Area { get; set; }
public bool Equals(TsvDataModel other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return ElementUid == other.ElementUid && Location == other.Location && RoomKey == other.RoomKey &&
SubType == other.SubType;
}
// current equality comparison implementation
public override bool Equals(object obj) => ReferenceEquals(this, obj) || (obj is TsvDataModel other && Equals(other));
public override int GetHashCode() => HashCode.Combine(ElementUid, Location, RoomKey, SubType);
}
What I want is to create dynamic equality based on user-provided properties i.e. sometimes I want to compare only by ElementIds and sometimes to include/exclude other properties. I don't want to create all possible combinations. I tried using reflection to get properties via dynamic type and I can get value and type, but comparison is not working.
I would like to use either IEqualityComparer or IEquitable (I'm using this object as HashSet<T>).
CodePudding user response:
You could create a simple factory class for producing IEqualityComparer<T> for you:
public static class DynamicEqualityComparer<T>
{
public static DynamicEqualityComparer<T, TSelector> Create<TSelector>(
Func<T, TSelector> selector)
=> new DynamicEqualityComparer<T, TSelector>(selector);
}
public class DynamicEqualityComparer<T, TSelector> : IEqualityComparer<T>
{
private readonly Func<T, TSelector> _Selector;
public DynamicEqualityComparer(Func<T, TSelector> selector)
{
_Selector = selector ?? throw new ArgumentNullException(nameof(selector));
}
public int GetHashCode(T value) => _Selector(value).GetHashCode();
public bool Equals(T a, T b) => _Selector(a).Equals(_Selector(b));
}
To create an equality comparer that compares Location and RoomKey you would do this:
DynamicEqualityComparer<TsvDataModel>.Create(dm => (dm.Location, dm.RoomKey))
and one for RoomKey and Area:
DynamicEqualityComparer<TsvDataModel>.Create(dm => (dm.RoomKey, dm.Area))
Note that there is absolutely no error checking in the code here so you would have to add it if you think about using it.
CodePudding user response:
Here's a DynamicEqualityComparer:
public class DynamicEqualityComparer<T> : IEqualityComparer<T>
{
private readonly IEnumerable<Func<T, object>> _getPropertiesToCompare;
public DynamicEqualityComparer(params Func<T, object>[] getPropertiesToCompare)
{
_getPropertiesToCompare = getPropertiesToCompare;
}
public bool Equals(T x, T y)
{
return _getPropertiesToCompare.All(getProperty =>
object.Equals(getProperty(x), getProperty(y)));
}
public int GetHashCode(T obj)
{
var hashCode = new HashCode();
foreach (var getProperty in _getPropertiesToCompare)
{
hashCode.Add(getProperty((T)obj));
}
return hashCode.ToHashCode();
}
}
Usage:
var comparer = new DynamicEqualityComparer<TsvDataModel>(
model => model.Location,
model => model.RoomKey,
model => model.Area);
var hashset = new HashSet<TsvDataModel>(comparer);
