2013-08-20 27 views
0

對我來說這似乎很奇怪。所有的定義都符合,但似乎在輸入array<Object^>^時調用函數時出現問題。這裏是我的簡單代碼:System.Reflection.TargetParameterCountException'發生在System.Windows.Forms.dll

void WriteCOMPortPannel(array< String^>^){ \\ Do something}; 
delegate void WriteCOMPortDelegate(array< String^>^); 

array <String^ > ^COM_PORTS = this -> SerialPort ->GetPortNames();   
this->Invoke (gcnew WriteCOMPortDelegate(this, &MainForm::WriteCOMPortPannel), COM_PORTS); 

在C#中的解決方案是:

this->Invoke (MyDeligate , New Object() COM_PORTS);.

什麼C++ \ CLI?是否有任何類型不匹配?

回答

1

我想C++/CLI Invoke錯誤您的array<String^>^array<Object^>^它期望擁有一個參數列表。

您應該嘗試在array<Object^>^內包裝您的array<String^>^

array <String^ > ^COM_PORTS = this -> SerialPort ->GetPortNames();   
array <Object^ > ^parameters = gcnew array <Object^ >(1); 
parameters[0] = COM_PORTS; 
this->Invoke (gcnew WriteCOMPortDelegate(this, &MainForm::WriteCOMPortPannel), parameters); 
+0

很好。謝謝 ! –

+0

另一個問題,爲什麼在另一個線程中調用這個 - > SerialPort-> GetPortNames()時沒有出現錯誤? 要了解更多信息,我知道SerialPort類不支持調用! –

+1

'SerialPort'類不是一個控件,所以它可能不是單線程綁定的(這意味着它不需要*'Invoke')。 – Medinoc

相關問題