执行您自己的自定义 .NET 事件处理循环

发布于 2024-07-06 09:31:33 字数 500 浏览 7 评论 0原文

几年前,我读过一本书,其中描述了如何使用自己的处理器覆盖 .NET 中的默认事件“调度程序”实现。

    class foo {
       public event EventHandler myEvent;
       ...
    }

    ...
      myFoo.myEvent += myBar1.EventHandler;
      myFoo.myEvent += myBar2.EventHandler;

每当事件触发时,myBar1 和 myBar2 处理程序都会被调用。

我记得,此循环的默认实现使用链接列表,并简单地迭代该列表并按顺序调用 EventHandler 委托。

我的问题有两个:

  1. 有人知道我在读哪本书吗?
  2. 为什么要覆盖默认实现(这可能会在书中得到解答)?

编辑:我指的书确实是 Jeffrey Richter 的 CLR via C#

A few years ago, I read a book that described how you could override the default event 'dispatcher' implementation in .NET with your own processor.

    class foo {
       public event EventHandler myEvent;
       ...
    }

    ...
      myFoo.myEvent += myBar1.EventHandler;
      myFoo.myEvent += myBar2.EventHandler;

Whenever the event fires, both myBar1 and myBar2 handlers will be called.

As I recall, the default implementation of this loop uses a linked list and simply iterates over the list and calls the EventHandler delegates in order.

My question is two fold:

  1. Does someone know which book I was reading?
  2. Why would you want to override the default implementation (which might be answered in the book)?

Edit: The book I was referring to was indeed Jeffrey Richter's CLR via C#

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青春有你 2024-07-13 09:31:33

它可能是许多书籍或网络文章中的一本。

您可能想要更改事件订阅/取消订阅方式的原因有多种:

  • 如果您有很多事件,其中许多事件很可能未被订阅,您可能需要使用 EventHandlerList 来降低内存使用量
  • 您可能希望记录订阅/取消订阅
  • 您可能希望使用弱引用避免订阅者的生命周期与您的生命周期绑定
  • 您可能希望更改与订阅/取消订阅相关的锁定

我确信还有更多 - 这些都超出了我的想象:)

编辑:另请注意,拥有一个处理订阅/取消订阅的自定义方式以及引发事件的自定义方式(例如,可以调用 GetInitationList 并保证调用所有处理程序,无论异常如何)。

It could have been one of many books or web articles.

There are various reasons why you might want to change how events are subscribed/unsubscribed:

  • If you have many events, many of which may well not be subscribed to, you may want to use EventHandlerList to lower your memory usage
  • You may wish to log subscription/unsubscription
  • You may wish to use a weak reference to avoid the subscriber's lifetime from being tied to yours
  • You may wish to change the locking associated with subscription/unsubscription

I'm sure there are more - those are off the top of my head :)

EDIT: Also note that there's a difference between having a custom way of handling subscription/unsubscription and having a custom way of raising the event (which may call GetInvocationList and guarantee that all handlers are called, regardless of exceptions, for example).

怪我入戏太深 2024-07-13 09:31:33

我似乎记得 Jeffrey Richter 的 CLR via C# 中有类似的内容。 编辑:我确实记得他对此进行了详细介绍。

控制事件注册有几个不同的原因。 其中之一是当您有大量事件时减少代码膨胀。 我认为杰弗里在书中详细讨论了这一点......

I seem to remember something similar in Jeffrey Richter's CLR via C#. Edit: I definitely do remember that he goes into detail about it.

There are a few different reasons for taking control of event registration. One of them is to reduce code bloat when you've got TONS of events. I think Jeffrey went into this in detail within the book...

天煞孤星 2024-07-13 09:31:33
  1. 例如,您可能需要根据处理程序之一的结果来中断调用链。 假设您的 CustomEventArgs 对象有一个属性“Blocked”,当设置为 true 时,会抑制所有进一步的事件处理程序调用。
  1. No
  2. You might, for example, need to break the call chain based on the result of one of the handlers. Say your CustomEventArgs object has a property 'Blocked', which when set to true suppresses all further event handler invocations.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文