Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Property Change Notification with Compile Time Safety

0.00/5 (No votes)
9 Dec 2014 1  
Simple trick to provide compile time safety for client side classes using INotify to notify when property changes (used generally by XAML bindings)

Introduction

Here is a cool little trick to ensure compile time safety when raising property change notification events. Instead of using strings as in "PropertyName", we can very well use a lamda expression like () => PropertyName.

Background

Property change notification is generally used in applications where XAML binding is used and hence these events are using to ensure two way binding with the UI as in MVVM design pattern.

Using the Code

Below is a typical implementation of INotifyPropertyChanged interface which provides no compile time safety and client object passes string to notify of a change in the property:

public class SampleClass : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    
    private string _name;
    
    public string Name
    {
        get { return _name; }
        set
        {
            if (_name == value) return;
            _name = value;
            OnPropertyChanged("Name");
        }
    }
    
    protected virtual void OnPropertyChanged(string propertyName)
    {
        if (propertyName != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

So, in order to ensure compile time safety, we provide a simple overload for OnPropertyChanged method as below, in turn using a helper method, which can very well be hosted in its own helper class. For the purpose of this example, I have it below:

protected virtual void OnPropertyChanged<T>(Expression<Func<T>> propertyExpression)
{
    OnPropertyChanged(ExtractPropertyName(propertyExpression));
}

private string ExtractPropertyName<T>(Expression<Func<T>> propertyExpression)
{
    return (((MemberExpression)(propertyExpression.Body)).Member).Name;
}

And now, the client properties can be changed to raise the events like this:

public string Name
{
    get { return _name; }
    set
    {
        if (_name == value) return;
        _name = value;
        OnPropertyChanged(() => Name);
    }
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here