Lovin’ .NET 4.5’s CallerMemberNameAttribute

Check this out:

http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.callermembernameattribute(v=vs.110).aspx

Hate the INotifyPropertyChanged pattern in .NET, and especially the syntax of needing to pass the name of the property being changed (via a string or an Expression or …)?

There’s finally a built-in solution in .NET 4.5!

private string _testValue;
public string TestValue
{
    get { return _testValue; }
    set {
        if (_testValue != value)
        {
            _testValue = value;
            RaisePropertyChanged();
        }            
    }
}

protected void RaisePropertyChanged([CallerMemberName] String propertyName = null)
{
    var eventHandler = this.PropertyChanged;
    if (eventHandler != null)
    {
        eventHandler(this, new PropertyChangedEventArgs(propertyName));
    }
}

A new attribute you can apply to optional parameters which emits the name of the Caller! So in the example above, it’s “TestValue.”

Rock on!