Is there any way to specify the allowed subclasses for an interface?
Let’s say I have this classes:
public interface IDoable // specify that only A or it's subclasses can implement IDoable
{
void Do();
}
public class A : IDoable
{
public void Do(){};
public void Foo(){};
}
public class B : IDoable // implementing IDoable on something other than A or its sublcasses doesn't make any sense
{
// public void Do();
}
So later I will be able to do this:
IDoable d = new A();
d.Do();
d.Foo(); // I'd like to be able to do this.
Does C# support this kind of feature?
You can cast
dtoAin order to be able to callFooon it:Note that if
dis notA, this will throw an exception.Other options are to test the type of
dusingisorasoperators.This of course goes against the point of using an interface to begin with.