C#事件处理程序 [英] C# Event Handlers

查看:95
本文介绍了C#事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果button.Click事件有任何处理程序关联,我如何检查C#?
如果(button.Click!= null)抛出编译错误。

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

推荐答案

你不能。事件只是暴露添加一个处理程序和删除一个处理程序 - 就是这样。 (事实上​​,在CLR中,您还可以使用元数据将方法与触发事件相关联,但是C#编译器从不会生成该事件。)某些事件发布者可能会提供其他方法来检查是否有任何订阅者你看到那些订阅者),但它不是事件模式本身的一部分。

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).

这篇关于C#事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆