문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top