c# - 无参数方法签名与参数委托(delegate)不匹配

标签 c# delegates compiler-errors params

为什么不:

delegate void MyDelegate(params object[] parameters);
static void ShouldMatch() {}
MyDelegate compilerError = ShouldMatch;

编译?看起来它应该匹配得很好。

最佳答案

委托(delegate),MyDelegate定义了一个接受对象数组的方法,但是你的 ShouldMatch方法没有。假设您尝试将任何参数传递给您的委托(delegate)实例,如下所示:

compilerError(someObject, someOtherObject);

如果方法compilerError必然不接受任何参数,你希望这里发生什么?

尝试以与委托(delegate)签名匹配的方式定义您的方法:
delegate void MyDelegate(params object[] parameters);
static void ShouldMatch(params object[] parameters) {}

MyDelegate noCompilerError = ShouldMatch;

或者您可以尝试将其包装在 lambda 表达式中,如下所示:
delegate void MyDelegate(params object[] parameters);
static void ShouldMatch() {}

MyDelegate noCompilerError = (paramArray) => ShouldMatch();

关于c# - 无参数方法签名与参数委托(delegate)不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19530777/

相关文章:

c# - 空消息异常

c# - 如何创建引用 Excel 中名称的 Excel 范围对象 (c#)

go - 语法错误 : unexpected name, 需要分号或换行符或 }

c++ - 找不到 opencv_world341d.dll 错误

visual-c++ - vc++ 6.0 中的 long long int 编译错误

c# - DotNet.Highcharts : Cost not plotted against the correct date

c# - 如何从 Azure Active Directory 获取客户端 secret 以使用一个驱动器业务 API 的 native 应用程序?

C# - 一个 Action<> 是否可以有多个方法签名?

c# - WinForms 调用/BeginInvoke

c# - 在运行时(不是编译时)用具有不同签名的委托(delegate)包装委托(delegate)