这个代表用法的目的是什么?

Kev*_*Kev 4 .net c# delegates

虽然使用.NET Reflector为应用程序搜索某些代码我没有源代码,但我发现:

if (DeleteDisks)
{
  using (List<XenRef<VDI>>.Enumerator enumerator3 = list.GetEnumerator())
  {
    MethodInvoker invoker2 = null;
    XenRef<VDI> vdiRef;
    while (enumerator3.MoveNext())
    {
      vdiRef = enumerator3.Current;
      if (invoker2 == null)
      {
        //
        // Why do this?
        //
        invoker2 = delegate {
          VDI.destroy(session, vdiRef.opaque_ref);
        };
      }
      bestEffort(ref caught, invoker2);
    }
  }
}
if (caught != null)
{
  throw caught;
}


private static void bestEffort(ref Exception caught, MethodInvoker func)
{
  try
  {
    func();
  }
  catch (Exception exception)
  {
    log.Error(exception, exception);
    if (caught == null)
    {
      caught = exception;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

为什么不VDI.destroy()直接打电话?try { do something } catch { log error }如果它被大量使用,这只是一种包装相同模式的方式吗?

Jar*_*Par 5

原因似乎是有一个函数可以处理和记录可能失败的操作中的错误:bestEffort.委托用于包装可能失败的操作并将其传递给bestEffort函数.