I realise this has been asked before, but I didn't find a clear agreement on the best solution.
Is using dynamic (like below) the best way to do this? I guess its best to avoid dynamic whenever possible to help catch problems at compile-time.
(classA1 and classA2 implement Interface A and likewise for B)
public static void Foo(InterfaceA a, InterfaceB b)
{
Foo((dynamic)a, (dynamic)b);
}
static void Foo(classA1 a, classB1 b) { //some code }
static void Foo(classA2 a, classB2 b) { //some code }
static void Foo(classA1 a, classB2 b) { //some code }
static void Foo(classA2 a, classB1 b) { //some code }
Or similarly...
public static void Foo(InterfaceA a, InterfaceB b)
{
((dynamic) a).Foo(b);
}
public classA1
{
void Foo(classB1 b) { //some code }
}
//repeated for other cases