You could have your MyObject<T>
take an equality comparer for your type T
and use that:
class MyObject<T> : IEquatable<MyObject<T>>
{
private readonly IEqualityComparer<T> comparer;
public MyObject(string otherProp, T value, IEqualityComparer<T> comparer)
{
this.comparer = comparer;
}
public MyObject(string otherProp, T value)
: this(otherProp, value, EqualityComparer<T>.Default)
{
}
public bool Equals(MyObject<T> other)
{
return OtherProp.Equals(other.OtherProp) && comparer.Equals(this.Value, other.Value);
}
}
Then for IEnumerable<T>
you can use a comparer which compares the sequences instead of the references. You might want to make use of factory methods to create your objects to ensure the same comparer type is used for the same T
to ensure equality remains symmetric.