[英]Parameterless method signature doesn't match params delegate
Why doesn't: 为什么不:
delegate void MyDelegate(params object[] parameters);
static void ShouldMatch() {}
MyDelegate compilerError = ShouldMatch;
Compile? 编译? It seems like it should match just fine.
看起来应该匹配得很好。
The delegate, MyDelegate
defines a method that takes an array of objects, but your ShouldMatch
method does not. 委托
MyDelegate
定义了一个采用对象数组的方法,但您的ShouldMatch
方法却没有。 Suppose you try to pass any parameters to an instance of your delegate like this: 假设您尝试像这样将任何参数传递给委托的实例:
compilerError(someObject, someOtherObject);
If the method compilerError
is bound to does not accept any parameters, what would you expect to happen here? 如果绑定到方法
compilerError
的方法不接受任何参数,那么您希望在这里发生什么?
Try defining your method in a way that matches the delegate signature: 尝试以与委托签名匹配的方式定义方法:
delegate void MyDelegate(params object[] parameters);
static void ShouldMatch(params object[] parameters) {}
MyDelegate noCompilerError = ShouldMatch;
Or you could try wrapping it in a lambda expression, like this: 或者您可以尝试将其包装在lambda表达式中,如下所示:
delegate void MyDelegate(params object[] parameters);
static void ShouldMatch() {}
MyDelegate noCompilerError = (paramArray) => ShouldMatch();
params
is a purely compile-time feature. params
是纯粹的编译时功能。
Delegate binding ignores it. 委托绑定将忽略它。
Your delegate must match the method's parameters exactly, ignoring params
& optional parameters. 您的委托人必须完全匹配方法的参数,而忽略
params
和可选参数。
The spec states this explicitly, in §6.6: 规范在第6.6节中明确指出:
o The candidate methods considered are only those methods that are applicable in their normal form (§7.5.3.1), not those applicable only in their expanded form.
o考虑的候选方法仅是那些以其正常形式(第7.5.3.1节)适用的方法,而不是仅以其扩展形式适用的那些方法。
§7.5.3.1 says: §7.5.3.1说:
For a function member that includes a parameter array, if the function member is applicable by the above rules, it is said to be applicable in its normal form.
对于包含参数数组的功能成员,如果该功能成员符合上述规则,则可以正常形式使用。 If a function member that includes a parameter array is not applicable in its normal form, the function member may instead be applicable in its expanded form:
如果包含参数数组的功能成员不能以其正常形式使用,则该功能成员可以以其扩展形式使用:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.