在我的代码中,我使用BeginInvoke
引发事件,但由于每个事件都有不同的EventArgs
,因此我的代码中包含重复的函数,例如:
private void EndAsyncConnect(IAsyncResult iar)
{
var ar = (System.Runtime.Remoting.Messaging.AsyncResult)iar;
var invokedMethod = (EventHandler<InfoArgs>)ar.AsyncDelegate;
invokedMethod.EndInvoke(iar);
}
private void EndAsyncReceived(IAsyncResult iar)
{
var ar = (System.Runtime.Remoting.Messaging.AsyncResult)iar;
var invokedMethod = (EventHandler<ReceivedArgs>)ar.AsyncDelegate;
invokedMethod.EndInvoke(iar);
}
有没有办法让EndAsync函数对所有事件都通用?
答案 0 :(得分:1)
如果您的所有活动代理类型都是EventHandler<T>
,那么这应该有效:
void CompletionCallback<T>(IAsyncResult iar)
where T : EventArgs
{
var ar = (System.Runtime.Remoting.Messaging.AsyncResult)iar;
var invokedMethod = (EventHandler<T>)ar.AsyncDelegate;
invokedMethod.EndInvoke(iar);
}