Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
问题描述
下面的代码块导致错误:TargetParameterCountException is unhandled by user code.参数计数不匹配.
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],
}
);
}
}
有什么想法吗?
推荐答案
由于数组协方差导致的错误;一个字符串数组可分配给 object[]
.这会导致 Invoke
方法将 string
数组的每个元素视为应该是 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});
(或)
Invoke(new Action<string[]>(AddListViewItem), (object)Data);
这让 Invoke
清楚地知道目标方法采用 single 参数.
This makes it crystal-clear to Invoke
that the target method takes a single parameter.
这篇关于参数计数与调用不匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:参数计数与调用不匹配?


基础教程推荐
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01