Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of Attributes... INotifyPropertyChanged

Tags:

c#

.net

wcf

wpf

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..

like image 529
jmayor Avatar asked Nov 02 '09 18:11

jmayor


People also ask

What is the use of INotifyPropertyChanged in C#?

The INotifyPropertyChanged interface is used to notify clients, typically binding clients, that a property value has changed. For example, consider a Person object with a property called FirstName .

How do you change Inotify property?

To implement INotifyPropertyChanged you need to declare the PropertyChanged event and create the OnPropertyChanged method. Then for each property you want change notifications for, you call OnPropertyChanged whenever the property is updated.

What is OnPropertyChanged C#?

INotifyPropertyChanged interface is used to notify the view or ViewModel that it does not matter which property is binding; it is updated. Let's take an example for understanding this interface. Take one WPF Window in which there are a total of three fields: First Name, Last Name and Full Name.


2 Answers

In case anyone happens across this thread and is using C# 5 (VS 2012+, .NET 4.5+). This case be done "more easily" now with CallerMemberNameAttribute. This attribute is applied to a string parameter and causes the compiler to pass in the name of the calling method/property when the default value us used (i.e. when and argument is not passed). Making implementing INotifyPropertyChanged less tiresome. For example:

public class MyClass
{
    private string myProperty;

    public string MyProperty
    {
        get { return myProperty; }
        set
        {
            myProperty = value;
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

So, you just need OnPropertyChanged() in each setter to send out the event and don't have to deal with a hard-coded string for the property name.

like image 149
Peter Ritchie Avatar answered Nov 15 '22 14:11

Peter Ritchie


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"

like image 36
Agent_9191 Avatar answered Nov 15 '22 15:11

Agent_9191