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:
0 comments:
Post a Comment