This is a mirror of official site: http://jasper-net.blogspot.com/

The New .NET 4.5 Feature Every XAML Developer Will Love

| Monday, September 10, 2012
If you develop using XAML and you are using .NET 4.5 (i.e. WPF or Windows 8) then there is a feature that will make you smile a bit, CallerMemberName. XAML developers often implement INotifyPropertyChanged to enable updating of data bound fields. If are smart, you often wrap the raising of the event into a simple method you can call, for example:

public void RaisePropertyChange(string propertyName)
{
    if (PropertyChanged != null)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

This leads to code that looks like this:

private int ticks;

public int Ticks
{
    get { return ticks; }
    set
    {
        if (ticks != value)
        {
            ticks = value;                    
            RaisePropertyChange("Ticks");
        }
    }
}

...
...

The solution: CallerMemberName
.NET 4.5 includes a new parameter attribute called System.Runtime.CompilerServices.CallerMemberName which will automatically place the name of the calling member (i.e. method or property) into the parameter. This enables us to change the method signature to:

public void RaisePropertyChange([CallerMemberName] string propertyName = "")

Read more: DZone
QR: Inline image 1

Posted via email from Jasper-net

0 comments: