向EventArgs添加属性

时间:2010-06-06 10:08:01

标签: c#

我通过添加字符串属性(fileToTest)扩展了标准的FileSystemWatcher类,现在我还需要扩展FileSystemEventArgs以添加此属性,我该怎么做?

我的扩展FileSystemWatcher:

    class AleFileSystemWatcher : FileSystemWatcher
{
    public string fileToTest { get; set; } 
}

FileSystemEventArgs fileToTest属性应与AleFileSystemWatcher fileToTest相同。

我可以这样做吗?

1 个答案:

答案 0 :(得分:1)

就个人而言,我不会扩展FileSystemWatcher,而是将其作为类中的实例变量。您并没有真正扩展FileSystemWatcher的主要功能,而是利用其功能(即监听已更改/创建的/无论何种文件,并将其与您正在查找的文件进行匹配)

public class SpecificFileWatcher
{
  public string FileToTest { get; set; }

  private readonly FileSystemWatcher iWatcher;


  public class SpecificFileWatcher(FileSystemWatcher watcher)
  {
    iWatcher = watcher;
    iWatcher.Changed += iWatcher_Changed; //whatever event you need here
  }

  //eventhandler for watcher
  public ...
  {
    if(e.FileName == FileToTest)
      Console.WriteLine("file found");
  }
}
相关问题