为什么不:
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/