How can I check in C# if button.Click event has any handlers associated? If (button.Click != null) throws compile error.
C# 事件处理程序
原标题:
问题回答
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.
我认为如果你在触发事件的类中,你就可以。
你可以定义处理程序并枚举每个处理程序。
例如,如果您的活动被定义为
event System.EventHandler NewEvent;
然后在触发事件方法中,您可以创建并执行...
EventHandler handler = NewEvent;
if(handler != null)
{
handler(this, e);
}
那将给你处理程序,从那里你可以获取调用列表。
EventDescriptor e = TypeDescriptor.GetEvents(yourObject).Find("yourEventName", true); 事件描述符e = TypeDescriptor.GetEvents(yourObject).Find("yourEventName", true);
相关问题
热门标签
- winforms
- combobox
- fogbugz
- java
- date
- internationalization
- asp.net
- iis
- url-rewriting
- urlrewriter
- c#
- enums
- ocaml
- haxe
- algorithm
- string
- viewstate
- .net
- c++
- c
- symbol-table
- mysql
- database
- postgresql
- licensing
- migration
- vb.net
- vb6
- declaration
- vb6-migration
- python
- psycopg2
- backup
- vmware
- virtualization
- gnu-screen
- authentication
- desktop
- excel
- xll
- cultureinfo
- regioninfo
- oracle
- client
- session
- download
- html
- virtual
- constructor
- scenarios
- perl
- full-text-search
- javascript
- ajax
- testing
- oop
- inheritance
- vim
- encapsulation
- information-hiding