C# Event Handlers

前端 未结 4 1300
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

    Why do you need this? What is the context? Maybe there's a better way to achieve the result
    The button is an external object and what you're trying to do is check is its internal list of subscribers without asking it. It's violating encapsulation..
    You should always let the object manage the subscribers for the events it exposes. If it wanted clients to be aware, it would have exposed a method HasClientsRegistered. Don't break in.

提交回复
热议问题