使用对象发送器和EventArgs调用c#方法
本文关键字:EventArgs 调用 方法 对象 | 更新日期: 2023-09-27 18:02:25
如何调用下面的方法手册?
private void NetworkResponseReceived(object sender, Network.ResponseReceivedEventArgs e)
{
ExecuteTask();
}
,例如:
NetworkResponseReceived();
需要在括号内插入什么?
显然你可以这样调用
NetworkResponseReceived(null, new ResponseReceivedEventArgs());
但这将是谎言。处理程序期望收到响应(来自某个对象),这不是真的。我认为最好将处理程序的逻辑提取到单独的方法中。并从代码和handler中调用那个方法。这样的:
void NetworkResponseReceived(object sender, Network.ResponseReceivedEventArgs e)
{
// this method can have some arguments, if needed
DoSomethingBusinessRelated();
}
从其他地方调用相同的方法:
DoSomethingBusinessRelated();
应该可以:
NetworkResponseReceived(null, new Network.ResponseReceivedEventArgs());
试试;
NetworkResponseReceived(null, new ResponseReceivedEventArgs());