What is the purpose of this delegate usage?

Posted by Kev on Stack Overflow See other posts from Stack Overflow or by Kev
Published on 2010-12-22T16:14:30Z Indexed on 2010/12/22 16:54 UTC
Read the original article Hit count: 163

Filed under:
|

Whilst poking around some code in .NET Reflector in an app I don't have the source code for I found this:

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;
    }
  }
}

Why not call VDI.destroy() directly. Is this just a way of wrapping the same pattern of try { do something } catch { log error } if it's used a lot?

© Stack Overflow or respective owner

Related posts about c#

Related posts about delegates