我遇到了一个问题。我有类似的东西:
public class ListSorter : MonoBehaviour
{
public delegate void MyDelegate();
public static MyDelegate myDelegate;
void Start ()
{
ListSorter.myDelegate += A(5);// <-- here I know i cant do it
ListSorter.myDelegate += B;
myDelegate(); //<-- how to call 2 different function with one delegate?
}
public void A(int iVar)
{
print(iVar);
}
public void B()
{
///...
}
}
正如您所见,我已经知道我想要在哪里以及有多少参数。问题是当函数A有参数而B没有参数时,如何调用函数A和B?
最佳答案
将函数调用包装在与目标类型匹配的匿名委托(delegate)中(无参数,无返回值):
ListSorter.myDelegate += (() => A(5));
ListSorter.myDelegate += B;
关于c# - 具有不同参数数量的方法的委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32013389/