FileSystemWatcher 事件无法被 System.Delegate 触及?
我正在编写涉及 FileSystemWatcher 对象的集成测试。为了让事情变得更容易,我想取消订阅事件委托的所有内容,而不必搜索每个订阅。我已经看到相关帖子,是否有必要取消订阅活动? 。这有点重复,但我特别询问为什么这不适用于 FileSystemWatcher 对象。
最好执行如下操作:
private void MethodName()
{
var watcher = new FileSystemWatcher(@"C:\Temp");
watcher.Changed += new FileSystemEventHandler(watcher_Changed);
watcher.Changed = null; // A simple solution that smells of C++.
// A very C#-ish solution:
foreach (FileSystemEventHandler eventDelegate in
watcher.Changed.GetInvocationList())
watcher.Changed -= eventDelegate;
}
无论如何引用 Changed 事件,编译器都会报告: 事件“System.IO.FileSystemWatcher.Changed”只能出现在+=或-=的左侧
类中的事件时,上面的代码工作得很好:
public event FileSystemEventHandler MyFileSystemEvent;
private void MethodName()
{
MyFileSystemEvent += new FileSystemEventHandler(watcher_Changed);
MyFileSystemEvent = null; // This works.
// This works, too.
foreach (FileSystemEventHandler eventDelegate in
MyFileSystemEvent.GetInvocationList())
watcher.Changed -= eventDelegate;
}
当使用同一 ,我缺少什么?看来我应该能够对 FileSystemWatcher 事件执行相同的操作。
I am writing integration tests that involve FileSystemWatcher objects. To make things easier, I want to unsubscribe everything from an event delegate without having to hunt down every subscription. I already saw related post, Is it necessary to unsubscribe from events?. This is somewhat a duplicate, but I am specifically asking why this doesn't work with a FileSystemWatcher object.
It would be nice to do something like the following:
private void MethodName()
{
var watcher = new FileSystemWatcher(@"C:\Temp");
watcher.Changed += new FileSystemEventHandler(watcher_Changed);
watcher.Changed = null; // A simple solution that smells of C++.
// A very C#-ish solution:
foreach (FileSystemEventHandler eventDelegate in
watcher.Changed.GetInvocationList())
watcher.Changed -= eventDelegate;
}
No matter how the Changed event is referenced, the compiler reports:
The event 'System.IO.FileSystemWatcher.Changed' can only appear on the left hand side of += or -=
The above code works just fine, when working with an event in the same class:
public event FileSystemEventHandler MyFileSystemEvent;
private void MethodName()
{
MyFileSystemEvent += new FileSystemEventHandler(watcher_Changed);
MyFileSystemEvent = null; // This works.
// This works, too.
foreach (FileSystemEventHandler eventDelegate in
MyFileSystemEvent.GetInvocationList())
watcher.Changed -= eventDelegate;
}
So, what am I missing? It seems that I should be able to do the same with the FileSystemWatcher events.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您在类中声明事件时,它(几乎)与以下代码等效:
请注意,事件没有
set
或get
访问器(如属性)并且你不能明确地写出它们。当你在类中编写
MyFileSystemEvent = null
时,它实际上是在执行_eventBackingField = null
,但是在你的类之外没有办法直接设置这个变量,你只有事件添加
&删除
访问器。这可能是一个令人困惑的行为,因为在类内部您可以通过事件名称引用事件处理程序委托,而不能在类外部执行此操作。
When you declare event in your class, it is an equivalent (almost) of the following code:
Notice that there is no
set
orget
accessor for event (like for properties) and you can't explicitly write them.When you write
MyFileSystemEvent = null
in your class, it is actually doing_eventBackingField = null
, but outside your class there is no way to directly set this variable, you have only eventadd
&remove
accessors.This might be a confusing behavior, because inside your class you can reference an event handler delegate by event name, and can't do that outside the class.
简而言之,
+=
和-=
是公共运算符,而=
是声明事件的类的私有运算符。Short answer is
+=
and-=
are public operators while=
is a private operator to the class that's declaring the event.