![](/img/trans.png)
[英]C# Reflection Invoke - Object of Type 'XXX' Cannot Be Converted to type 'System.Object[]'
[英]Exception “Object of type 'System.Object[]' cannot be converted to type”
我是C#的新手。 从另一个类中运行的线程更新WPF中的文本框时遇到问题。
我的例外是:
类型为'System.Object []'的对象不能转换为类型
这是我的代码形式:
public void UpdateUserOnline(userOnlineClass message)
{
if(onLineTextbox.Dispatcher.CheckAccess())
{
// The calling thread owns the dispatcher, and hence the UI element
for (int i = 0; i < message.user.Length; i++)
{
// this.Dispatcher.Invoke(Action)
onLineTextbox.AppendText("•" + message.user + "\r\n");
onLineTextbox.AppendText(" ->" + message.status + "\r\n");
// txtDestination.Items.Add(message.user[i]);
}
}
else
{
// Invokation required
onLineTextbox.Dispatcher.Invoke(DispatcherPriority.Normal,new UpdateUserOnlineCallback(UpdateUserOnline), new Object[] { message } );
}
}
这是我的Thread类中的代码的一部分:
thrMessaging = new Thread(new ThreadStart(ReceiveMessages));
thrMessaging.Start();
private void ReceiveMessages()
{
Response = null;
while (chatClient.Connected)
{
if (strm.DataAvailable)
{
Response = (commandClass)reciveFormatter.Deserialize(strm);
processMessage(Response);
//procProcessMessageCallback(processMessage), new object[] { Response });
//ProcessMessageCallback proccess = new ProcessMessageCallback(processMessage(Response));
}
}
}
private void processMessage(commandClass message)
{
/*
if (message.money != null)
{
UpdateUserRanking((userRankingClass)message.money);
}
*/
if (message.online != null)
{
waitinglist.UpdateUserOnline((userOnlineClass)message.online);
}
}
onLineTextbox.Dispatcher.Invoke(
DispatcherPriority.Normal,
new UpdateUserOnlineCallback(UpdateUserOnline),
new Object[] { message } );
您使用了错误的重载方法。
您正在使用invoke(priority,delegate,object)-但具有对象数组。
如果要传递对象数组,请交换优先级并进行委派,如下所示:
onLineTextbox.Dispatcher.Invoke(
new UpdateUserOnlineCallback(UpdateUserOnline),
DispatcherPriority.Normal,
new Object[] { message } );
您正在使用的过载期待的对象,而不是使用Object []
来源: http : //msdn.microsoft.com/en-us/library/ms591596.aspx和http://msdn.microsoft.com/en-us/library/cc647499.aspx
Dispatcher.Invoke重载有点奇怪。 有一个单参数重载,一个多参数重载使第一个参数与其余参数的params数组中的参数分开。 使用“消息”作为第三个参数而不将其包装在数组中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.