Suppose I need to inherit from two classes in C#. This is not allowed, so I can do the following: inherit from one of the classes and include the other class as a member variable, inherit from its interfaces and reimplement all methods of those interfaces by redirecting them onto that member variable:
interface SecondBaseInterface {
void FirstMethod();
void SecondMethod();
};
class MyClass : FirstBaseClass, SecondBaseInterface {
public void FirstMethod()
{
secondBase.FirstMethod();
}
public void SecondMethod()
{
secondBase.SecondMethod();
}
SecondBaseClass secondBase = new SecondBaseClass();
};
now this works, but if there’s a lot of methods in SecondBaseInterface it will require lot of typing of code that in fact does nothing useful and I’ll have to maintain that code in case SecondBaseInterface changes.
Is there some C# feature that would tell the compiler “for all methods of SecondBaseInterface please call corresponding methods of this member variable” or some other convenient way to do such massive redirection?
There is no simple language feature that springs to mind, here are a few ideas: