views:

137

answers:

2

I find myself writing this code a lot:

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

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

I'm wondering whether it might be possible to write an attribute that could be added to the property to automatically raise the event. I.e. something like this:

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

Before I go and try to implement this I was wondering:

  • Is this facility in the .net framework
  • Has anyone tried to this facility
  • If it's possible
  • If there are any dead ends that I should avoid
+5  A: 

To do that, you would need an AOP framework for .NET, like PostSharp or AOP.NET

Philippe Leybaert
A: 

I you are prepared to use a helper class to wrap the property values you can do this. But that means any client accessing the property will need to unwrap the value.

Another route is to use a helper type, see WPF and WF using (different) DependencyProperty for this. But you don't get automatically implemented properties.

Richard