Ok, I’m trying to do the following:
protected bool ValidAdvert(Base item)
{
throw ThisIsAnAbstractClassException();
}
protected bool ValidAdvert(Derived1 item)
{
return ADerived1SpecificPredicate;
}
protected bool ValidAdvert(Derived2 item)
{
return ADerived2SpecificPredicate;
}
And have the Derived class versions of the method be called, when a Base class is passed to the method. The base class is abstract so this should, in theory, be possible?
Before someone says something about overloading the method on the classes themselves, the logic inside the methods relies on a large number of different conditions, none of which are related, and none of which related to the Base/Derived class directly (such as login status, etc.)
If you find the double dispatch too intrusive, you can call the method by reflection and resolve the proper overload dynamically.
This pattern is called MultipleDispatch, while calling a method by reflection is slower than calling the method directly (it won’t be an overhead if the method is called less than few hundred times per second), it has the benefit of not requiring to modify your class hierarchy like in the Double Dispatch pattern.
This will be even easier when c# 4.0 comes out with the dynamic keyword: