So you want to inherit all the functionalities of the FileSystemWatcher
, while adding your properties?
Try inheriting the class:
public class MyFileSystemWatcher : FileSystemWatcher
{
public string XmlConfigPath { get; set; }
}
and after that you can use your new class on every place where you would use the System.IO.FileSystemWatcher
, like this:
MyFileSystemWatcher fsw = new MyFileSystemWatcher();
fsw.XmlConfigPath = @"C:\config.xml";
fsw.Path = @"C:\Watch\Path\*.*";
Another approach would be to make a class that will both have the config file location , and the FileSystemWatcher object as a property, along these lines:
public class MyFileSystemWatcherManager
{
public string XmlConfigPath { get; set; }
public FileSystemWatcher Watcher { get; set; }
public MyFileSystemWatcherManager()
{
Watcher = new FileSystemWatcher();
}
}
and with usage like this:
MyFileSystemWatcherManager manager = new MyFileSystemWatcherManager();
manager.XmlConfigPath = @"C:\config.xml";
manager.Watcher.Path = @"C:\Watch\Path\*.*";
Composition is possible even when the base class cannot be inherited, and it's generally preferred from an OO perspective, since most of the code that uses your class will rely only on your class, and not on the base class. However, when you do want to inherit all of the base behaviours and just add some (well-defined) extras, inheritance is the simpler and more tightly-coupled solution.