从 C++/CLI 引发事件的正确方法?
我想知道从 C++/CLI 引发事件的正确方法是什么。 在 C# 中,首先应复制处理程序,检查它是否不为空,然后调用它。 C++/CLI 有类似的做法吗?
I was wondering what's the proper way of raising events from C++/CLI. In C# one should first make a copy of the handler, check if it's not null, and then call it. Is there a similar practice for C++/CLI?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这不是故事的全部! 您通常不必担心 C++/CLI 中的空事件处理程序。 这些检查的代码是为您生成的。 考虑以下简单的 C++/CLI 类。
如果您编译此类,并使用 Reflector 反汇编它,您将获得以下 c# 代码。
通常的检查是在 raise 方法中完成的。 除非您确实想要自定义行为,否则您应该像在上面的类中一样轻松地声明您的事件,并引发它而不必担心空处理程序。
This isn't the whole story! You don't usually have to worry about null event handlers in C++/CLI. The code for these checks is generated for you. Consider the following trivial C++/CLI class.
If you compile this class, and disassemble it using Reflector, you get the following c# code.
The usual checks are being done in the raise method. Unless you really want custom behavior, you should feel comfortable declaring your event as in the above class, and raising it without fear of a null handler.
C++/CLI 允许您覆盖 中的
raise
自定义事件处理程序,这样您就不必在引发事件时测试null
或复制。 当然,在您的自定义raise
中,您仍然需要执行此操作。示例,改编自 MSDN 以确保正确性:
C++/CLI allows you to override
raise
in custom event handlers so you don't have to test fornull
or copy when raising the event. Of course, inside your customraise
you still have to do this.Example, adapted from the MSDN for correctness:
如果您的问题是 raise 不是私有的,请按照文档所述显式实现它:
http://msdn.microsoft.com/en-us/library/5f3csfsa.aspx
总之:
如果您只是使用事件 关键字,您创建了一个“琐碎”事件。 编译器生成add/remove/raise以及您的代表成员。 生成的 raise 函数(如文档所述)检查 nullptr。 此处记录了一些琐碎事件:
http://msdn.microsoft.com/en-us/library/4b612y2s。 aspx
如果您想要“更多控制”,例如将 raise 设为私有,那么您必须显式实现链接中所示的成员。 您必须显式声明委托类型的数据成员。 然后,您使用 event 关键字来声明与事件相关的成员,如 Microsoft 示例中所示:
Wordy,但确实如此。
-赖利。
If your issue is that raise isn't private, then explicitly implement it like the docs say:
http://msdn.microsoft.com/en-us/library/5f3csfsa.aspx
In summary:
If you just use the event keyword, you create a "trivial" event. The compiler generates add/remove/raise and the delegate member for you. The generated raise function (as the docs say) checks for nullptr. Trivial events are documented here:
http://msdn.microsoft.com/en-us/library/4b612y2s.aspx
If you want "more control", for example to make raise private, then you have to explicitly implement the members as shown in the link. You must explicitly declare a data member for the delegate type. Then you use the event keyword to declare the event-related members, as in the Microsoft example:
Wordy, but there it is.
-reilly.