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");
}
}
精彩评论