是否可以使用属性在属性更改时自动引发事件

2024-01-08

我发现自己经常写这样的代码:

    private int _operationalPlan;
    public int OperationalPlan
    {
        get
        {
            return _operationalPlan;
        }
        set
        {
            _operationalPlan = value;
            RaisePropertyChanged();
        }
    }

    private void RaisePropertyChanged()
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new
                                      PropertyChangedEventArgs("PlansSelected"));
        }
    }

我想知道是否可以编写一个可以添加到属性中的属性来自动引发事件。 IE。像这样的东西:

[RaiseOnSet("ProperyChanged", "PropertyChangedEventArgs", "PlansSelected")]
public int OperationalPlan
{
    get
    {
        return _operationalPlan;
    }
    set
    {
        _operationalPlan = value;
        RaisePropertyChanged();
    }
}

在我尝试实现这个之前,我想知道:

  • 此功能是否在 .net 框架中
  • 有没有人尝试过这个设施
  • 如果它是可能的
  • 如果有什么我应该避免的死胡同

为此,您需要一个 .NET 的 AOP 框架,例如后锐利 https://www.postsharp.net/aop.net or AOP.NET http://sourceforge.net/projects/aopnet

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

是否可以使用属性在属性更改时自动引发事件 的相关文章

随机推荐