Reputation: 12966
I am struggling to think of real-world usage of C# 5's Caller Information feature
In what scenarios do you need to know who called your method? What other usages does it have other than tracing and possibly debugging?
Is it an implementation of Aspect Oriented Programming?
Upvotes: 2
Views: 352
Reputation: 1043
One really nice use for CallerMemberName
is when working with WPF and databinding.
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
is a convenient implementation for INotifyPropertyChanged
.
Upvotes: 8
Reputation: 2140
I am not sure whether it's AOP, but to me, it's very useful when implementing INotifyPropertyChanged. Actually Microsoft does give a nice sample about the difference between 4.0 and 4.5:
4.5: http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx
4.0: http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.100).aspx
it's pretty useful, especially when you do a lot databinding, either in WPF or WinForm. it reduces the use of error prone of hardcoding the property name, though you could use reflection to achieve similar behaviour.
Upvotes: 2