From Stackoverflow, I've picked up some useful code to remove magic strings from my application. The code looks like this:
protected void OnPropertyChanged<T>(Expression<Func<T>> selectorExpression) { }
The method is called by:
this.OnPropertyChanged( () => DisplayName);
(Where DisplayName
is a property of the class.)
Using methods of this form is really useful (I like how it is easy to refactor) but I don't really understand what is is doing.
Where is T
assigned, and what value does it take? How does intellisense pick up which properties the class has (is T
implicitly set from the context in which the method is called?)
Is the () => DisplayName
expression fixed at compile time, or is it calculated each time the method is called? Related: is using magic strings more efficient?