C # loop for each event internal to the class declaration

I saw such code somewhere.

Ideally, I would like this loop to fit this type of Func event.

public static event Func<RecentDirectories, DirectoryInfo, Exception, bool> ContinueOnExceptionEvent;

/// <summary>
/// Determine if the loop should continue on a general exception not already handled
/// in the loop catch statement.
/// </summary>
/// <param name="dir"></param>
/// <param name="e"></param>
/// <returns>True continues loop, false rethrows the exception</returns>
protected virtual bool TryContinueOnException(DirectoryInfo dir, Exception ex)
{
    if (!Aborted) // check if thread aborted before doing event
    {
        if (null != ContinueOnExceptionEvent)
        {
            // foreach line doesn't compile because 
            // ContinueOnExceptionEvent doesn't have a GetEnumerator()
            foreach (var e in ContinueOnExceptionEvent)
            {
                if (e(this, dir, ex))
                {
                    return true;
                }
            }
        }
    }

    return false;
}

How do I get foreach to get all events and repeat them?

+3
source share
1 answer

You can access each subscriber by calling GetInvocationList.

protected virtual bool TryContinueOnException(DirectoryInfo dir, Exception ex)
{
    if (!Aborted)
    {
        var e = ContinueOnExceptionEvent;
        if (e != null)
        {
            var ds = e.GetInvocationList();
            foreach (Func<RecentDirectories, DirectoryInfo, Exception, bool> d in ds)
            {
                if (d(this, dir, ex))
                    return true;
            }
        }
    }
    return false;
}
+2
source

All Articles