C# Event Handlers

前端 未结 4 1209
Happy的楠姐
Happy的楠姐 2021-01-17 23:32

How can I check in C# if button.Click event has any handlers associated? If (button.Click != null) throws compile error.

4条回答
  •  走了就别回头了
    2021-01-17 23:48

    I think you can if you are in the class that raises the event.

    You can define the handler and enumerate each.

    e.g. If your event is defined as

    event System.EventHandler NewEvent;
    

    Then on the raise event method you might create you can do...

        EventHandler handler = NewEvent;
        if(handler != null)
        {
          handler(this, e);
        }
    

    That will give you the handler and from that you can get the Invocation List.

提交回复
热议问题