The problem is that when the
Equals
method is called from either the==
override, it callsObject.Equals()
and notCoordinate.Equals()
.
No, the problem is that those things are different. If they are the same, as they should be, then there's no problem.
So make them the same. Don't make the derived class do the wrong thing; force them to do the right thing.
public abstract class ValueObject<T> : IEquatable<T>
{
// Force the derived class to override these.
public abstract override bool Equals(object obj);
public abstract override int GetHashcode(object obj);
// And then consistently use the overridden method as the implementation.
public virtual bool Equals(T obj)
{
return obj1.Equals((object)obj2);
}
public static bool operator ==(ValueObject<T> obj1, ValueObject<T> obj2)
{
return obj1.Equals((object)obj2);
}
public static bool operator !=(ValueObject<T> obj1, ValueObject<T> obj2)
{
return !obj1.Equals((object)obj2);
}
}