c# - 具有不同参数数量的方法的委托(delegate)

标签 c# parameters delegates

我遇到了一个问题。我有类似的东西:

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/

相关文章:

c# - 类型的 sizeof() 运算符

ios - Swift:从其他 Viewcontroller 调用 PageViewController 中的函数

c# - Windows 窗体应用程序中异常处理的最佳实践?

c# - System.InvalidOperationException : Value must be set. 为 SQLite 设置空参数

rust - 我无法理解 Rust "scope"定义(Rust 编程语言,第二版。Klabnik & Nichols)

flash - 如何在 Delphi 2010 中将参数传递给 flash 电影?

android - 动态设置布局参数

ios - 具有相同数据源和委托(delegate)函数的多个对象

ios - UIStackView 在 Swift 中没有成员委托(delegate)错误

c# - 如何用c#读取XML元素(提供示例代码)