使用 .NET 是否可以将自定义属性分配给内置对象(例如 FileSystemWatcher)?

发布于 2024-09-11 14:11:02 字数 206 浏览 1 评论 0原文

是否可以将自定义属性添加到属于 .NET 框架一部分的对象?

我知道如果我只是提供一个我编写的属性的类,我会如何执行此操作,但是向 FileSystemWatcher 类添加自定义属性怎么样?

我正在加载我想要从 XML 文件观看的路径,但还想添加一个属性来存储更多信息,在本例中是配置文件的位置。我可以将此属性添加到 FileSystemWatcher 类本身吗?

Is it possible to add a custom property to an object that is part of the .NET framework?

I know how I would do this if I was just giving a class i'd wrote a property, but what about adding a custom property to the FileSystemWatcher class?

I'm loading in the Path I want to watch from an XML file, but also want to add a property to store more information, in this case the location of a config file. Can I add this property to the FileSystemWatcher class itself?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

埋情葬爱 2024-09-18 14:11:02

您想在添加属性的同时继承FileSystemWatcher的所有功能吗?
尝试继承该类:

public class MyFileSystemWatcher : FileSystemWatcher
{
    public string XmlConfigPath { get; set; }
}

然后您可以在每个需要使用 System.IO.FileSystemWatcher 的地方使用新类,如下所示:

MyFileSystemWatcher fsw = new MyFileSystemWatcher();
fsw.XmlConfigPath = @"C:\config.xml";
fsw.Path = @"C:\Watch\Path\*.*";

另一种方法是创建一个类,该类将同时具有配置文件位置 和 FileSystemWatcher 对象作为属性,遵循以下原则:

public class MyFileSystemWatcherManager
{
    public string XmlConfigPath { get; set; }
    public FileSystemWatcher Watcher { get; set; }

    public MyFileSystemWatcherManager()
    {
        Watcher = new FileSystemWatcher();
    }
}

并使用如下所示:

MyFileSystemWatcherManager manager = new MyFileSystemWatcherManager();
manager.XmlConfigPath = @"C:\config.xml";
manager.Watcher.Path = @"C:\Watch\Path\*.*";

即使基类无法继承,组合也是可能的,并且从 OO 角度来看通常首选它,因为大多数代码使用您的类将仅依赖于您的类,而不依赖于基类。但是,当您确实想要继承所有基本行为并仅添加一些(定义良好的)额外功能时,继承是更简单且更紧密耦合的解决方案。

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.

等数载,海棠开 2024-09-18 14:11:02

围绕您要使用的组件创建一个包装器。

Create a wrapper around the component you want to use.

征﹌骨岁月お 2024-09-18 14:11:02

不幸的是,扩展属性在 C# 中不存在。您需要创建一个继承FileSystemWatcher 的子类。

Unfortunately, extension properties do not exist in c#. You need to create a child class that inherits FileSystemWatcher.

最美不过初阳 2024-09-18 14:11:02

我必须承认,我并不真正理解您想要做什么,但是您可以可以像这样解决您的问题:

public static class FileSystemWatcherExtensions
{
    public static Dictionary<string, string> MyProperty {get;set;}

    public static string GetMyProperty(this FileSystemWatcher watcher)
    {
        if (MyProperty != null && MyProperty.ContainsKey[watcher.GetHashCode()]) {
            return FileSystemWatcherExtensions.MyProperty[watcher.GetHashCode()];
        } else {
            return null;
        }
    }

    public static void SetMyProperty(this FileSystemWatcher watcher, string value)
    {
        if (MyProperty == null) {
            MyProperty = new Dictionary<string, string>();
        }
        FileSystemWatcherExtensions.MyProperty[watcher.GetHashCode()] = value;
    }
}
// I changed this example to allow for instance methods - but the naming can be
// improved...

这将创建两个扩展方法作为您的属性的 getter/setter,您可以像这样使用:

var fsw = new FileSystemWatcher();
fsw.SetMyProperty("a string");
var val = fsw.GetMyProperty(); // val == "a string"

这意味着您仍然可以保持语法就像您实际上是在向 FileSystemWatcher 类添加一个属性您只调用该类而不是您的分机。然而,您实际上只是将自己包装在扩展类的静态属性周围。

I must admit that I don't really understand what you're trying to do, but you could possibly solve your problem like this:

public static class FileSystemWatcherExtensions
{
    public static Dictionary<string, string> MyProperty {get;set;}

    public static string GetMyProperty(this FileSystemWatcher watcher)
    {
        if (MyProperty != null && MyProperty.ContainsKey[watcher.GetHashCode()]) {
            return FileSystemWatcherExtensions.MyProperty[watcher.GetHashCode()];
        } else {
            return null;
        }
    }

    public static void SetMyProperty(this FileSystemWatcher watcher, string value)
    {
        if (MyProperty == null) {
            MyProperty = new Dictionary<string, string>();
        }
        FileSystemWatcherExtensions.MyProperty[watcher.GetHashCode()] = value;
    }
}
// I changed this example to allow for instance methods - but the naming can be
// improved...

This would create two extension methods that act as getter/setter for your property, that you use like so:

var fsw = new FileSystemWatcher();
fsw.SetMyProperty("a string");
var val = fsw.GetMyProperty(); // val == "a string"

This means you can still keep your syntax as if you're actually adding a property to the FileSystemWatcher class in that you're only calling on that class and not on your extension. However, you're really just wrapping yourself around a static property on an extension class.

暖树树初阳… 2024-09-18 14:11:02

创建您自己的从 FileSystemWatcher 派生的类怎么样?您可以自由添加属性。

What about creating your own class derived from FileSystemWatcher? Youll be free to add properties.

ヅ她的身影、若隐若现 2024-09-18 14:11:02

继承的替代方法是装饰目标类的包装类。
此处查看装饰器模式

An alternative to inheriting would be a wrapper class which decorates the target class.
Check out the decorator pattern here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文