Gou*_*oul 5 c# exception multicastdelegate
我已经获得了一些通过多播委托调用的代码。
我想知道如何赶上并管理那里引发的任何异常,但目前尚未管理。我无法修改给定的代码。
我一直在四处寻找,发现需要调用 GetInitationList() 但不太确定这是否有帮助。
小智 5
考虑使用以下代码GetInvocationList:
foreach (var handler in theEvent.GetInvocationList().Cast<TheEventHandler>()) {
// handler is then of the TheEventHandler type
try {
handler(sender, ...);
} catch (Exception ex) {
// uck
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的旧方法,我更喜欢的新方法是上面的,因为它使调用变得简单,包括使用 out/ref 参数(如果需要)。
foreach (var singleDelegate in theEvent.GetInvocationList()) {
try {
singleDelgate.DynamicInvoke(new object[] { sender, eventArg });
} catch (Exception ex) {
// uck
}
}
Run Code Online (Sandbox Code Playgroud)
它单独调用每个被调用的委托
theEvent.Invoke(sender, eventArg)
Run Code Online (Sandbox Code Playgroud)
快乐编码。
请记住在处理事件时执行标准的空保护复制检查(也许还有锁定)。
| 归档时间: |
|
| 查看次数: |
3076 次 |
| 最近记录: |