使用 .NET 是否可以将自定义属性分配给内置对象(例如 FileSystemWatcher)?
是否可以将自定义属性添加到属于 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您想在添加属性的同时继承
FileSystemWatcher
的所有功能吗?尝试继承该类:
然后您可以在每个需要使用 System.IO.FileSystemWatcher 的地方使用新类,如下所示:
另一种方法是创建一个类,该类将同时具有配置文件位置 和 FileSystemWatcher 对象作为属性,遵循以下原则:
并使用如下所示:
即使基类无法继承,组合也是可能的,并且从 OO 角度来看通常首选它,因为大多数代码使用您的类将仅依赖于您的类,而不依赖于基类。但是,当您确实想要继承所有基本行为并仅添加一些(定义良好的)额外功能时,继承是更简单且更紧密耦合的解决方案。
So you want to inherit all the functionalities of the
FileSystemWatcher
, while adding your properties?Try inheriting the class:
and after that you can use your new class on every place where you would use the
System.IO.FileSystemWatcher
, like this: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:
and with usage like this:
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.
围绕您要使用的组件创建一个包装器。
Create a wrapper around the component you want to use.
不幸的是,扩展属性在 C# 中不存在。您需要创建一个继承FileSystemWatcher 的子类。
Unfortunately, extension properties do not exist in c#. You need to create a child class that inherits FileSystemWatcher.
我必须承认,我并不真正理解您想要做什么,但是您可以可以像这样解决您的问题:
这将创建两个扩展方法作为您的属性的 getter/setter,您可以像这样使用:
这意味着您仍然可以保持语法就像您实际上是在向
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:
This would create two extension methods that act as getter/setter for your property, that you use like so:
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.创建您自己的从 FileSystemWatcher 派生的类怎么样?您可以自由添加属性。
What about creating your own class derived from FileSystemWatcher? Youll be free to add properties.
继承的替代方法是装饰目标类的包装类。
此处查看装饰器模式
An alternative to inheriting would be a wrapper class which decorates the target class.
Check out the decorator pattern here