C# Event Handlers

前端 未结 4 1276
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:38

    You can't. Events just expose "add a handler" and "remove a handler" - that's all. (In fact in the CLR you can also have metadata to associate a method with "fire the event" but the C# compiler never generates that.) Some event publishers may offer additional means to check whether or not there are any subscribers (or indeed let you see those subscribers) but it's not part of the event pattern itself.

    See my article about events for more information, or look at the events tag (which I'm about to add to this question).

提交回复
热议问题