安全调用多播委托
曾经想要调用多播委托,但是你希望调用整个调用列表,即使链中的任何一个发生异常也是如此。那么你很幸运,我已经创建了一个扩展方法来实现这一点,只有在执行整个列表完成后抛出 AggregateException
:
public static class DelegateExtensions
{
public static void SafeInvoke(this Delegate del,params object[] args)
{
var exceptions = new List<Exception>();
foreach (var handler in del.GetInvocationList())
{
try
{
handler.Method.Invoke(handler.Target, args);
}
catch (Exception ex)
{
exceptions.Add(ex);
}
}
if(exceptions.Any())
{
throw new AggregateException(exceptions);
}
}
}
public class Test
{
public delegate void SampleDelegate();
public void Run()
{
SampleDelegate delegateInstance = this.Target2;
delegateInstance += this.Target1;
try
{
delegateInstance.SafeInvoke();
}
catch(AggregateException ex)
{
// Do any exception handling here
}
}
private void Target1()
{
Console.WriteLine("Target 1 executed");
}
private void Target2()
{
Console.WriteLine("Target 2 executed");
throw new Exception();
}
}
这输出:
Target 2 executed
Target 1 executed
直接调用,没有 SaveInvoke
,只会执行目标 2。