My new favorite C# language feature: [CallerMemberName]
With the release of Visual Studio 2012 and C# 4.5, [CallerMemberName] makes its entrance. With this new awesome feature you can write INotifyPropertyChanged code without having to worry about renaming properties and keeping strings in sync for the call to OnPropertyChanged(…).
This is how a property with INotifyPropertyChanged support looks like pre C# 4.5:
public string OldStylePropertyChanged
{
get { return _oldStyle; }
set
{
if (value != _oldStyle)
{
_oldStyle = value;
OnPropertyChangedOldStyle("OldStylePropertyChanged");
}
}
}
private void OnPropertyChangedOldStyle(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
And this is how it will look like when we apply the use of [CallerMemberName] in our PropertyChanged method:
public string DynamicTitle
{
get { return _dynamicTitle; }
set
{
if (value != _dynamicTitle)
{
_dynamicTitle = value;
OnPropertyChanged();
}
}
}
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
Some more really cool additions to [CallerMemberName] is the following Parameter attributes:
[CallerMemberName] [CallerFilePath] [CallerLineNumber]
The name of the attributes is straight forward and easy to understand, so I doubt that I would have to describe them in detail, instead, let’s look at an example straight from MSDN on how to use them.
public sealed class Logger
{
public void TraceMessage(string message,
[CallerMemberName] string memberName = "",
[CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0)
{
Debug.WriteLine("message: " + message);
Debug.WriteLine("member name: " + memberName);
Debug.WriteLine("source file path: " + sourceFilePath);
Debug.WriteLine("source line number: " + sourceLineNumber);
}
}
Happy hacking!











1 comment
Thanks
This is very nice feature. Thanks for provided example. I guess I'm gonno fall in love (again)
Post new comment