执行您自己的自定义 .NET 事件处理循环
几年前,我读过一本书,其中描述了如何使用自己的处理器覆盖 .NET 中的默认事件“调度程序”实现。
class foo {
public event EventHandler myEvent;
...
}
...
myFoo.myEvent += myBar1.EventHandler;
myFoo.myEvent += myBar2.EventHandler;
每当事件触发时,myBar1 和 myBar2 处理程序都会被调用。
我记得,此循环的默认实现使用链接列表,并简单地迭代该列表并按顺序调用 EventHandler 委托。
我的问题有两个:
- 有人知道我在读哪本书吗?
- 为什么要覆盖默认实现(这可能会在书中得到解答)?
编辑:我指的书确实是 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:
- Does someone know which book I was reading?
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它可能是许多书籍或网络文章中的一本。
您可能想要更改事件订阅/取消订阅方式的原因有多种:
我确信还有更多 - 这些都超出了我的想象:)
编辑:另请注意,拥有一个处理订阅/取消订阅的自定义方式以及引发事件的自定义方式(例如,可以调用 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:
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).
我似乎记得 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...