C# 向EventArgs添加属性
我通过添加一个字符串属性(fileToTest)扩展了标准FileSystemWatcher类,现在我还需要扩展FileSystemEventArgs来添加这个属性。我如何才能做到这一点 我的扩展文件系统监视程序:C# 向EventArgs添加属性,c#,C#,我通过添加一个字符串属性(fileToTest)扩展了标准FileSystemWatcher类,现在我还需要扩展FileSystemEventArgs来添加这个属性。我如何才能做到这一点 我的扩展文件系统监视程序: class AleFileSystemWatcher : FileSystemWatcher { public string fileToTest { get; set; } } FileSystemEventArgs fileToTest属性应与AleFileS
class AleFileSystemWatcher : FileSystemWatcher
{
public string fileToTest { get; set; }
}
FileSystemEventArgs fileToTest属性应与AleFileSystemWatcher fileToTest相同
我可以这样做吗?就我个人而言,我不会扩展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");
}
}