我正在读这本书,我被困在这里:
public static class EventArgExtensions {
public static void Raise<TEventArgs>(this TEventArgs e,
Object sender, ref EventHandler<TEventArgs> eventDelegate)
where TEventArgs : EventArgs {
// Copy a reference to the delegate field now into a temporary field for thread safety
EventHandler<TEventArgs> temp =
Interlocked.CompareExchange(ref eventDelegate, null, null);
// If any methods registered interest with our event, notify them
if (temp ! = null) temp(sender, e);
}
}
Run Code Online (Sandbox Code Playgroud)
特别是MSDN doc说的那样
Run Code Online (Sandbox Code Playgroud)public static object CompareExchange(ref object location1, object value, object comparand)System.Threading.Interlocked的成员
摘要:
比较两个对象以获得引用相等性,如果它们相等,则替换其中一个对象.参数:
location1:与comparand进行比较并可能被替换的目标对象.
value:如果比较结果相等,则替换目标对象的对象.
comparand:与location1处的对象进行比较的对象.返回:
location1中的原始值.异常:
System.ArgumentNullException:location1的地址是空指针.
这意味着这是错误的:
EventHandler<TEventArgs> temp =
Interlocked.CompareExchange(ref eventDelegate, null, null);
// If any methods registered interest with our event, notify them
if (temp ! = null) temp(sender, e);
Run Code Online (Sandbox Code Playgroud)
因为我需要检查传递给CompareExchange的内容而不是输出.
我错过了什么吗?