I have
public abstract class DataClass
{
public static bool operator ==(DataClass left, DataClass right)
{
return left.Equals(right);
}
}
and this is what happens
object left = new DataClass();
object right = new DataClass();
bool expected = true;
bool actual;
actual = ((DataClass)left) == ((DataClass)right);
Assert.AreEqual(expected, actual); // passes
actual = left == right;
Assert.AreEqual(expected, actual); // fails
How to make it call the right implementation, without casting it explicitly?
staticmethods are not subject to polymorphic behavior (i.e. they cannot be overriden). The cast is required.For a possible workaround see this related question: Override a static method
Most likely you will have to resort to creating an instance method or overriding
Equalsinstead.