继承类中的事件隐藏不起作用

Jan*_*vil 5 c# events inheritance

我试图隐藏继承类中的事件,但不是通过 EditorBrowserable 属性隐藏事件。我有一个继承自FileSystemWatcher的DelayedFileSystemWatcher,我需要隐藏 Changed、Created、Deleted 和 Renamed 事件并将其设置为私有。我尝试了这个,但它不起作用:

    /// <summary>
    /// Do not use
    /// </summary>
    private new event FileSystemEventHandler Changed;
Run Code Online (Sandbox Code Playgroud)

XML 注释未显示在 IntelliSense 中(显示原始信息)。但是,如果我将访问修饰符更改为 public,则 XML 注释将显示在 IntelliSense 中。

欢迎任何帮助。

Han*_*ant 5

您不想使用它,但它可以轻松解决您的问题:

class MyWatcher : FileSystemWatcher {
    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
    private new event FileSystemEventHandler Changed;
    // etc..
}
Run Code Online (Sandbox Code Playgroud)

您唯一能做的就是封装它。这是可行的,该类没有那么多成员,并且您要消除其中的几个:

class MyWatcher : Component {
    private FileSystemWatcher watcher = new FileSystemWatcher();
    public MyWatcher() {
        watcher.EnableRaisingEvents = true;
        watcher.Changed += new FileSystemEventHandler(watcher_Changed);
        // etc..
    }
    public string Path {
        get { return watcher.Path; }
        set { watcher.Path = value; }
    }
    // etc..
}
Run Code Online (Sandbox Code Playgroud)