I have classes B
and C
inheriting from A
.
public class A { }
public class B : A { }
public class C : A { }
I have some functions that take objects B
and C
as arguments:
public void FunB(B obj) { }
public void FunC(C obj) { }
I want to create a function that accepts any of these functions as arguments. I try to use delegates, but I don't find the solution. I tried the this, but I get the following errors:
public class Test
{
public void FunB(B obj) { }
public void FunC(C obj) { }
public delegate void Delegate(A obj);
public static void Method(Delegate fun) { }
public void Pain()
{
Delegate funB = FunB; // No overload for FunB matches the delegate Test.Delegate
Delegate fun2 = (A obj) => { };
fun2 += FunB; // No overload for FunB matches the delegate Test.Delegate
Method(FunB); // Argument 1: cannot convert from 'method group' to 'Test.Delegate'
}
}
I read the documentation (that's why I tried with the fun2 += FunB
) but it's clear there is something I'm not understanding. Any idea how to fix it?