views:

47

answers:

4

I want to know if a control has a method assigned on the Click event.

Button b = new Button();
b.Click += (sender, e) => { };

/* What I want */
b.Click.Count // 1

I need something that can at least tell me if the control has or not some method on the click event.

+1  A: 

You can't do that. Event is just add/remove methods to add delegate to the chain. Only the class that has an event can know if there are subscribers to the event.

Even more, you should not need to know if there are subscribers to the event of a control. Why do you need this information?

Andrew Bezzub
My guess he is trying to avoid multiple subscription
PerlDev
+1  A: 

You might do some reading into this topic http://stackoverflow.com/questions/660480/determine-list-of-event-handlers-bound-to-event , it appears that it may be somewhat related to what you are trying to do.

Norman H
Reflection, I was almost sure that would be the only way, and I guess I was right... Thanks!
BrunoLM
A: 

If you need test if the delegate exists/subscribed, you can try -= first, in catch run +=

PerlDev
A: 

You can only do that from within the class that owns the event handler (Button in this case). Within the Button class you could test for Click != null to see if there were subscribers. Unfortunately, since I suspect the Button is the framework class, you're out of luck.

Dr Herbie