A quick code example shows the usage ( all code here is available as a GitHub:Gist)
The output from this demo is:
The new attributes are at lines 20, 21 and 22 of the code. As you can see in the example, to use one of the attributes, it must be used to decorate an optional parameter on a method - the parameter will be set to the correct value at runtime.
INotifyPropertyChanged Usage
One major benefit of the CallerMemberNameAttribute is in a class that implements the INotifyPropertyChanged interface. This interface has an event in the form of PropertyChanged(string propertyName), and will be called from each property's setter method when the value changes. This leads to a lot of magic strings in code, for example:
This brittle code can be avoided by using the attribute like this:
What members does it work with?
As well as methods, properties and events, the attributes will work for these member types:
- Constructor, value is ".ctor
- Explicit conversion operator, value is "op_Explicit"
- Implicit conversion operator, value is "op_Implicit"
- Indexer, value is "Item"
- Operator overload, value is e.g. "op_Addition"
- Finaliser, value is "Finalize"
Where does the info come from?
The attributes work by inspecting the IL of the calling code. For instance, the IL for the Main() method of my first example contains these items of info:
As I said before, all code here is available as a GitHub:Gist
No comments:
Post a Comment