C ++ / CLI事件有没有任何监听器? [英] Does C++/CLI event have any listeners?
问题描述
在C#中,我可以检查事件是否有任何监听器:
In C# I can check if an event has any listeners:
C#示例:
public static event EventHandler OnClick;
if (OnClick != null)
OnClick(null, new EventArgs() );
在C ++ / CLI中检查事件是否为null不是必需的。
In C++/CLI checking if the event is null is not necessary.
C ++ / CLI示例:
C++/CLI Example:
delegate void ClickDelegate( Object^ sender, MyEventArgs^ e );
event ClickDelegate^ OnClick;
OnClick (sender, args);
但是,在我正在开发的项目中,我不想构造MyEventArgs对象if没有监听器。
BUT, in the project I am working on, I don’t want to construct the MyEventArgs object if there are no listeners.
如何知道OnClick是否在C ++中有任何监听器?
How do I find out if OnClick has any listeners in C++?
推荐答案
似乎你不能用琐碎的事件检查,像你使用的,因为你没有直接访问基础字段(如在C#中的自动实现的属性)。
It seems you can't check that with "trivial events", like you used, because you don't have direct access to the underlying field (as with auto-implemented properties in C#).
如果要这样做,可以明确指定事件的访问器方法和返回字段。请参阅如何:定义事件访问器方法,了解如何做到这一点。
If you want to do this, you can specify the event's accessor methods and the backing field explicitly. See How to: Define Event Accessor Methods on how exactly to do that.
这篇关于C ++ / CLI事件有没有任何监听器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!