默认的事件添加/删除的实现事件

2023-09-04 01:07:21 作者:- Expect

我在寻找实现一些额外的逻辑,当事件处理程序被添加或删除一个事件。

I'm looking to implement some extra logic when event handlers are added or removed to an event.

我知道,在.net中的默认实现最近改变。

I'm aware that the default implementation in .net changed recently.

我想继续我的实现接近默认的实现成为可能。

I'd like to keep my implementation as close to the default implementation as possible.

任何人都可以点我的/提供的东西,说明如何在compliler实现事件?

Can anyone point me to/provide something that shows how the compliler implements events?

推荐答案

见的这个系列的博客文章的。

在C#< 4,它在锁定取值

In C# <4, it used simple delegate operations in locks.

在C#4+,它采用了发烧友的无锁算法,在循环中调用 Interlocked.CompareExchange ;看它在反编译器的更多细节。

In C# 4+, it uses a fancier lock-free algorithm by calling Interlocked.CompareExchange in a loop; look at it in a decompiler for more detail.

如果你的确定的,你的班会的永远的在多个线程中使用,你不需要任何的;你可以简单地使用不同步的委托算术。

If you're sure that your classes will never be used on multiple threads, you don't need any of that; you can simply use unsynchronized delegate arithmetic.

 
精彩推荐