I have the following base interface
public interface IBaseAction
{
bool CanAct(...)
}
and two inheriting interface say
public interface IAction1 : IBaseAction{}
and
public interface IAction2 : IBaseAction{}
My problem is, I have a class which implements both, and I want to implement CanAct DIFFERENTLY.
public class ComplexAction : IAction1, IAction2
{
bool IAction1.CanAct(...){} //doesn't compile as CanAct is not a member of IAction1!!
}
ComplexAction c=new ComplexAction();
var a1 = (IAction1)c;
var a2 = (IAction2)c;
a1.CanSave(); //THESE TWO CALLS SHOULD BE IMPLEMENTED DIFFERENTLY
a2.CanSave();
Is there a reasonably clean way to do this?
(Also, my interfaces have semantic meaning and at least three more functions, so it is out of the question to throw out the whole hierarchy, but I’d be willing to copy bool CanAct to every inheriting interface if that is the only solution (there are 4-6 of them))
And what the CLR is supposed to do if someone calls
((IBaseAction)a1).CanSave()? There could be just one implementation forIBaseAction.CanSave(). So I think you can’t do this conceptually.This is a fundamental problem of multiple inheritance called the diamond problem. The bottom line is: if you hit it, your type hierarchy design is definitely wrong.
E.g. in this particular case, you’re better off with the Role class model (also known as the Role pattern).