我正在从以下代码中收到 ArgumentException,我很难理解它堆栈跟踪中的最后一个条目是
System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr,
Binder binder, Object[] parameters, CultureInfo culture,
Boolean verifyAccess, StackCrawlMark& stackMark)
当我逐步完成 DeviceResponse 时,按预期填充,并且目标已按预期定位,但 targetForm.Invoke 每次都会抛出
任何帮助将非常感激。
事件定义为:
public static event EventHandler<MsgEventArgs<DeviceResponse>> DeviceResponseReceived;
该事件正在从此代码中引发:
//Raise the event
if (DeviceResponseReceived != null)
{
if (DeviceResponseReceived.Target is System.Windows.Forms.Form)
{
System.Windows.Forms.Form targetForm = DeviceResponseReceived.Target as System.Windows.Forms.Form;
targetForm.Invoke(DeviceResponseReceived, new MsgEventArgs<DeviceResponse>(deviceResponse));
}
}
MsgEventArgs 是派生自 EventArgs 的通用事件参数类:
public class MsgEventArgs<T> : EventArgs
{
public MsgEventArgs(T value)
{
m_value = value;
}
private T m_value;
public T Value
{
get { return m_value; }
}
}
在我的表单中,我已经在表单构造函数中注册了该事件:
DeviceResponse.DeviceResponseReceived += new EventHandler<MIASmartClient.Messaging.Transport.MsgEventArgs<DeviceResponse>>(DeviceResponse_DeviceResponseReceived);
实施为:
void DeviceResponse_DeviceResponseReceived(object sender, MIASmartClient.Messaging.Transport.MsgEventArgs<DeviceResponse> e)
{
_presenter.DeviceResponseReceived(e.Value);
}
感谢您花时间看一看