views:

155

answers:

1

Can I figure out if a function has already been assigned to an event?

e.g. (Standard winforms app with web browser control)

namespace Crawler {
    public partial class Form1 : Form {

        WebCrawler.manager m;

        public Form1() {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e) {
            system.windows.forms.webbrowser wb = new system.windows.forms.webbrowser();
            wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(foo);

            //[... Some other code ...]

            /* [Begin Example] */

            if (wb.DocumentCompleted.Contains(foo){

                // Behave Normally

            }else {

                // Do Something Else...

            }
        }
    }
}

And, if I can do something like I described above, then how?

+1  A: 

You can call Deletegate.GetInvocationList.

Here is an example:

using System;
using System.Linq;

class Program
{
    static event Action foo;

    static void bar() { }

    static void Main()
    {
     foo += bar;

     bool contains = foo
      .GetInvocationList()
      .Cast<Action>()
      .Contains(bar);
    } 
}
Andrew Hare
I'm now using this line: WebBrowser wb;List<Action> actions = wb.DocumentCompleted.GetInvocationList().Cast<Action>().ToList<Action>().Contains(wb_DocumentCompleted);But it gives me this error: "The event 'System.Windows.Forms.WebBrowser.DocumentCompleted' can only appear on the left hand side of += or -="
Sean Ochoa