How can I determine whether an event has been bound to an event handler?

Source: Internet
Author: User

// Bind an event for button1Program
Button BTN = new button ();
BTN. Click + = new eventhandler (button2_click );
// Obtain information about all events defined by the button class
Propertyinfo Pi = (typeof (button). getproperty ("Events", bindingflags. instance | bindingflags. nonpublic );
// Obtain the list of event handlers of the button object BTN
Eventhandlerlist EHL = (eventhandlerlist) PI. getvalue (BTN, null );
// Obtain the field information of the control Click Event
Fieldinfo = (typeof (Control). getfield ("eventclick", bindingflags. Static | bindingflags. nonpublic );
// Match the event handler list of the BTN object with the field information of the obtained click event to obtain the delegate object of the BTN object Click Event
// The event is defined by the delegate. The multicast delegate in C # can be bound to multiple event handlers. when an event occurs, these event handlers are executed in sequence.
// Therefore, the delegate object has a getinvocationlist method to obtain all the Event Handlers bound to this delegate.
Delegate d = EHL [fieldinfo. getvalue (null)];

Foreach (delegate del in D. getinvocationlist ())
{
// Determine whether an event handler has been bound to a click event
Console. writeline (Del. method. Name = "button#click ");
}

 

Private void button#click (Object sender, eventargs E)
{
MessageBox. Show ("Hello, button#click ");
}

Private void button2_click (Object sender, eventargs E)
{
MessageBox. Show ("Hello, button2_click ");
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.