简体   繁体   English

异常“类型'System.Object []'的对象不能转换为类型”

[英]Exception “Object of type 'System.Object[]' cannot be converted to type”

I'm a newbie for C#. 我是C#的新手。 I have a problem while updating textbox in WPF from thread that running in another class. 从另一个类中运行的线程更新WPF中的文本框时遇到问题。

My exception is: 我的例外是:

Object of type 'System.Object[]' cannot be converted to type 类型为'System.Object []'的对象不能转换为类型

Here is my code in form: 这是我的代码形式:

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 } );
    }
}

And this is my part of code from my Thread class: 这是我的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 } );

You're using the wrong overload method. 您使用了错误的重载方法。

You're using invoke(priority,delegate,object) - but with an object array. 您正在使用invoke(priority,delegate,object)-但具有对象数组。

if you want to pass an object array - swap your priority and delegate around, like so: 如果要传递对象数组,请交换优先级并进行委派,如下所示:

onLineTextbox.Dispatcher.Invoke(
    new UpdateUserOnlineCallback(UpdateUserOnline),
    DispatcherPriority.Normal,
    new Object[] { message } );

The overload you're using is expecting an Object, rather than Object[] 您正在使用的过载期待的对象,而不是使用Object []

Source: http://msdn.microsoft.com/en-us/library/ms591596.aspx and http://msdn.microsoft.com/en-us/library/cc647499.aspx 来源: http//msdn.microsoft.com/en-us/library/ms591596.aspxhttp://msdn.microsoft.com/en-us/library/cc647499.aspx

The Dispatcher.Invoke overloads are a bit strange. Dispatcher.Invoke重载有点奇怪。 There is a single argument overload, and a multiple argument overload that takes the first argument in a separate parameter from the params array of remaining arguments. 有一个单参数重载,一个多参数重载使第一个参数与其余参数的params数组中的参数分开。 Use "message" as the third argument without wrapping it in an array. 使用“消息”作为第三个参数而不将其包装在数组中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#反射调用-无法将类型为“ XXX”的对象转换为类型为“ System.Object []”的对象 - C# Reflection Invoke - Object of Type 'XXX' Cannot Be Converted to type 'System.Object[]' Nhibernate抛出&#39;System.Linq.EnumerableQuery`1 [Entity]&#39;类型的对象无法转换为类型&#39;System.Linq.IQueryable`1 [System.Object []]&#39; - Nhibernate throws Object of type 'System.Linq.EnumerableQuery`1[Entity]' cannot be converted to type 'System.Linq.IQueryable`1[System.Object[]]' 无法转换System.Object []错误 - System.Object[] error cannot be converted 无法将“ System.Object []”转换为类型“ System.String []” - Cannot convert 'System.Object[]' to the type 'System.String[]' 为什么 CreateSQLQuery 抛出异常“值 System.Object[] 不是类型,不能在此泛型集合中使用”? - Why CreateSQLQuery throwing exception "The value System.Object[] is not of type and cannot be used in this generic collection"? 类型'System.Int32'的表达式不能用于方法'Boolean Equals(System.Object)'的'System.Object'类型的参数 - Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' of method 'Boolean Equals(System.Object)' 类型&#39;System.Int32&#39;的表达式不能用于方法&#39;Boolean Equals(System.Object)&#39;的类型&#39;System.Object&#39;的参数 - Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' of method 'Boolean Equals(System.Object)' 为什么类型是System.Object? - Why the type is System.Object here? 什么时候特定类型实际上是System.Object? - When is a specific type actually a System.Object? 未定义或导入预定义类型System.Object - Predefined type System.Object is not defined or imported
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM