tags:

views:

214

answers:

2

This is just something I was thinking as I was learning on Attributes and I was using the INotifyPropertyChanged too much, is just and Idea and I would like to hear some opinios about it.( I know this would require some work on the compiler and not on the cosumer side)

Since INotifyPropertyChanged is used with the same Pattern most of the time .. just like calling the method that fire ups the event with the name of the property ,could it be designed as and Attribute and using Auto-Properties? So that the compiler knows it need to add the call to the PropertyChanged event? So if we have the class....

public class DemoCustomer : INotifyPropertyChanged
{
  public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String info)
    {
     if (PropertyChanged != null)
     {
      PropertyChanged(this, new PropertyChangedEventArgs(info));
     }
    }

  private string companyNameValue = String.Empty;
         ...
}

Instead of declaring the property

public string CompanyName
    {
     get
     {
      return this.companyNameValue;
     }

     set
     {
                       if (value != this.companyNameValue)
                       {
                          this.companyNameValue = value;
                          NotifyPropertyChanged("CompanyName");
                       }
     }
    }

we could do something like this if we can indicate to the compiler by this attribute that it needs to generate a call to the PropertyChanged with the name of the property if the new value is different from the previous

[NotifyPropertyChanged]
public string CompanyName
{
  get;set;
}

We could still keep coding in the old way for some custom behaviours when no using the Attribute..

+3  A: 

This style of thinking is called Aspect Oriented Programming (or AOP). You can achieve the end result by adding a post build action using Mono's Cecil to go through properties with that attribute and modify the behavior of the property and spit out the newly compiled assembly with the appropriate behavior. You can look at Jason Bock's Dimecast on "Leveraging Cecil to inject code into your Assemblies"

Agent_9191
Very good reading.. Thanks a lot... I started to use postsharp... that's why I'm pointing the other answer. But this is usefull as well.
jmayor
+5  A: 

You can do this with PostSharp, but I don't think it'll be in the core compiler any time soon.

Roger Lipscombe