What is the purpose of this delegate usage?
- by Kev
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?