开发者

adding a property to an EventArgs

I have extended the standard FileSystemWatcher class by adding a string property (fileToTest), now i need to extend also the FileSystemEventArgs to add this property how can i do this ?

my extend FileSystemWatcher :

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

The FileSystemEventArgs fileToTest property should be the same of AleFile开发者_Python百科SystemWatcher fileToTest .

Can i do this ?


Personally, I wouldn't extend the FileSystemWatcher, but have it as an instance variable in a class. You don't really extend the functionality of the FileSystemWatcher in its main respect, but make use of its functionality (ie. listen for changed/created/whatever files and match those with the file you are looking for)

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");
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜