C# - 拦截子类中的属性更改
我正在创建一个框架,在其中提供基类,框架的实现者将从基类继承并提供其他属性和方法。在基类中,我希望有一种方法来观察属性值何时更改。该属性可以来自基类或任何子类。我知道通过反射,我可以确定任何实例的属性列表,但是有没有办法可以跟踪属性更改值?
这是我所说的一个非常简单的示例:
public class BaseClass
{
public string BaseClassProperty { get; set; }
public void DoSomethingWhenEitherPropertyGetsChanged()
{
}
}
public class SubClass : BaseClass
{
public string SubClassProperty { get; set; }
}
当任一属性的值发生更改时,我该怎么做才能执行 DoSomethingWhenEitherPropertyGetsChanged
。
I'm in the process of creating a framework in which I provide the base class and the implementers of the framework will inherit from the base class and provide additional properties and methods. In the base class, I would like to have a way of observing when a property value is changed. The property can be from the base class or in any of the subclasses. I know that through reflection, I can determine the list of properties from any instance, but is there a way I can track the property changing value?
Here is a very simplistic example of what I am saying:
public class BaseClass
{
public string BaseClassProperty { get; set; }
public void DoSomethingWhenEitherPropertyGetsChanged()
{
}
}
public class SubClass : BaseClass
{
public string SubClassProperty { get; set; }
}
What can I do to have DoSomethingWhenEitherPropertyGetsChanged
get executed when either of the properties has it's value changed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
notifypropertyweaver
来实现此目的。它完全符合您的要求。这里有一个链接:来自开源主页:
使用 IL 编织(通过 http://www.mono-project.com/Cecil) 将 INotifyPropertyChanged 代码注入 特性。
You can use
notifypropertyweaver
for this purpose. It does exactly what you want. Here's a link:From the open source home page:
Uses IL weaving (via http://www.mono-project.com/Cecil) to inject INotifyPropertyChanged code into properties.
我可能会使用 Postsharp 并创建一个继承属性,将拦截代码注入所有公共属性中。将属性标记为继承还应该自动将其附加到所有子类。
I would probably use Postsharp and create an inherited attribute injecting interception code into all public properties. Marking the attribute as inherited should also attach it to all subclasses automatically.
我根据您的要求写了自己的想法,但我不确定它是否适合您的需求。 INotifyProperty 已更改,您也可以研究一下,但我不太喜欢它,因为它就像连接意大利面条一样。不过,也许这会给你一些创造性的想法。
它的作用是允许您将 ObservableObject 用作所有属性类型。通过这样做,每个属性都会有一个可以连接到的 ObjectChanged 事件。缺点是您必须在构造函数中初始化所有属性,以防止代码中出现 NullReferenceException。
此示例使用三个类。
ObservableObject.cs
Employee.cs
Program.cs
I wrote my own idea of your requirements, but I am not sure if it suits your needs. INotifyProperty changed is something you could also look into, but I don't really like it because it is like wiring up speghetti. Maybe this will give you some creative ideas, though.
What this does, is allow you to use ObservableObject as for all of your properties types. By doing this, each property will have an ObjectChanged event you can wire-up to. The con(s) are that you must initialize all of your properties in the constructor to prevent a NullReferenceException somewhere in your code.
This example uses three classes.
ObservableObject.cs
Employee.cs
Program.cs
您最好的选择是 CrisWue 推荐的方法,并使用 postsharp 或其他一些后处理器将行为注入到您的属性中。除此之外,我认为您需要在属性中手动调用 DoSomethingWhenEitherPropertyGetsChanged() 。
如果您要创建一个供您或您的组织以外的人使用的库,则后处理器可能不是正确的方法,因为它将第三方工具添加为构建过程的另一个要求。
Your best bet would be what CrisWue recommended and use postsharp or some other post-processor to inject the behavior in your properties. Other than that I think you would need to call DoSomethingWhenEitherPropertyGetsChanged() manually within your properties.
If you are creating a library that is consumed by people other than you or your organization, a post-processor may not be the right way to go as it adds the 3rd party tool as another requirement to their build process.