与调用参数数量不匹配? [英] Parameter count mismatch with Invoke?
本文介绍了与调用参数数量不匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下错误结果的代码块:TargetParameterCountException是由用户代码未处理。参数数量不匹配。
The code block below results in the error: TargetParameterCountException was unhandled by user code. Parameter count mismatch.
public void AddListViewItem(string[] Data)
{
if (InvokeRequired)
{
Invoke(new Action<string[]>(AddListViewItem), Data);
}
else
{
ListViewData.Items.Add(Data[0]).SubItems.AddRange
(
new string[]
{
Data[1],
Data[2],
Data[3],
}
);
}
}
任何想法?
Any ideas?
推荐答案
错误发生,因为数组协变的;字符串数组是分配给 [对象]
。这将导致调用
的方法来对待字符串中的每个元素
数组,就好像它应该是一个参数传递给 AddListViewItem
法
The error occurs because of array covariance; an array of strings is assignable to object[]
. This causes the Invoke
method to treat each element of the string
array as if it should be an argument to the AddListViewItem
method.
下面是一个修复:
Invoke(new Action<string[]>(AddListViewItem), new object[] {Data});
(或)
(or)
Invoke(new Action<string[]>(AddListViewItem), (object)Data);
这使得它清澈的至调用
该目标的方法需要的单的参数。
This makes it crystal-clear to Invoke
that the target method takes a single parameter.
这篇关于与调用参数数量不匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文