Sorry for the bad title, but I didn't know how to entitle this in a good way.
Let the following interfaces :
interface IFoo
{
}
interface IBar
{
}
And the following classes :
class Base
{
}
class Derived1 : Base, IFoo, IBar
{
}
class Derived2 : Base, IFoo, IBar
{
}
I need to have a common type for Derived1 and Derived2 that has all the features of Base, IFoo and IBar. so that i can have instances of both Derived1 and Derived2 into the same collection and have access to the features of Base, IFoo and IBar without doing some reflexion black magic.
If Base was an interface, I would just have to create an interface requiring to implement Base, IFoo and IBar, but it's not the case.
I also do not want to have an intermediate inheritance between Derived1 / Derived2 and Base :
class Intermediate : Base, IFoo, IBar
{
}
class Derived1 : Intermediate
{
}
class Derived2 : Intermediate
{
}
Because I'm in the context of Unity and inheritance is to be avoided there more than anywhere.
Also I cannot modify Base.
I thought about having an IBase interface that requires all the features of Base and have an "intermediate" interface requiring to implement it too, i.e :
interface IIntermediate : IBase, IFoo, IBar
{
}
class Derived1 : Base, IIntermediate
{
}
class Derived2 : Base, IIntermediate
{
}
But I would have to copy all of the features of Base (and its parents) into IBase and IBase would also not be a Base. So that looks a bit dirty and might cause me problems I don't see yet.
I there a different way to acheive what I need to do other than the two possibilities mentionned above ? Thank you !
Base, it's unclear why an additional level of inheritance would be undesirable.Base?Base?Baseis actuallyMonoBehaviour. I would like to avoid an extra level of inheritance, because when you declare a Unity "event" (such as Awake(), Update(), etc), you also have to manually call the parent one, if any. This is error prone and not nice to maintain.Baseand use Tuple with the type parameter ofBaseand your new Interface, but both indicate to same object. It's just a relief not a strong solution.