FileSystemWatcher 事件无法被 System.Delegate 触及?

发布于 2024-11-09 11:42:27 字数 1281 浏览 0 评论 0原文

我正在编写涉及 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 技术交流群。

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

发布评论

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

评论(2

提赋 2024-11-16 11:42:27

当您在类中声明事件时,它(几乎)与以下代码等效:

private FileSystemEventHandler _eventBackingField;
public event FileSystemEventHandler MyFileSystemEvent
{
    add
    {
        _eventBackingField =
            (FileSystemEventHandler)Delegate.Combine(_eventBackingField, value);
    }
    remove
    {
        _eventBackingField =
            (FileSystemEventHandler)Delegate.Remove(_eventBackingField, value);
    }
}

请注意,事件没有 setget 访问器(如属性)并且你不能明确地写出它们。

当你在类中编写MyFileSystemEvent = null时,它实际上是在执行_eventBackingField = null,但是在你的类之外没有办法直接设置这个变量,你只有事件添加 & 删除访问器。

这可能是一个令人困惑的行为,因为在类内部您可以通过事件名称引用事件处理程序委托,而不能在类外部执行此操作。

When you declare event in your class, it is an equivalent (almost) of the following code:

private FileSystemEventHandler _eventBackingField;
public event FileSystemEventHandler MyFileSystemEvent
{
    add
    {
        _eventBackingField =
            (FileSystemEventHandler)Delegate.Combine(_eventBackingField, value);
    }
    remove
    {
        _eventBackingField =
            (FileSystemEventHandler)Delegate.Remove(_eventBackingField, value);
    }
}

Notice that there is no set or get 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 event add & 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.

归途 2024-11-16 11:42:27

简而言之,+=-= 是公共运算符,而 = 是声明事件的类的私有运算符。

Short answer is += and -= are public operators while = is a private operator to the class that's declaring the event.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文