类型为"System.Reflection.TargetParameterCountException"的未处理异常

An unhandled exception of type 'System.Reflection.TargetParameterCountException'

本文关键字:未处理 TargetParameterCountException 异常 Reflection System 类型      更新时间:2023-10-16

我创建了一个BeginInvoke,这样我就可以从非UI线程写入文本框。线程A调用一个委托,该委托在线程A的上下文中运行testFunc。testFunc然后执行BeginInvoke,它运行空函数ControlBoxDelegateMethod。如果删除了BeginInvoke行,则程序将运行。但如果它被保留,我会得到以下异常:

mscorlib.dll中出现"System.Reflection.TargetParameterCountException"类型的未处理异常。其他信息:参数计数不匹配。

        private:
        //delegate void ControlBoxDelegate(Label^ myControl,int whichControl);
        void ControlBoxDelegateMethod(Label^ myControl,int whichControl)
        {
         //  myControl->Text = "Test!!!!!!!";
        }
        public: 
        void testFunc()
        {
            int which = 3;
            local_long_textBox->BeginInvoke(gcnew  ControlBoxDelegate
                           (this,&Form1::ControlBoxDelegateMethod),which);
        }

有人能告诉我我在这里做错了什么吗?谢谢

ControlBoxDelegateMethod接受两个参数(Label^int),但您只传递一个参数(名为whichint)。您缺少第一个参数。

所以,它可能应该是这样的:

local_long_textBox->BeginInvoke(gcnew ControlBoxDelegate(this,&Form1::ControlBoxDelegateMethod), your_label, which);