Вопрос

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