Feel free to correct me if this is a bad way of going about using static reflectionâ€¦ but I had a change to remove a â€˜magic stringâ€™ and figured Iâ€™d use static reflection as a way around it.
I was working on a Silverlight sample of Two-way binding and re-reading some of John Papaâ€™s Data-Driven Services with Silverlight 2 book where he is showing that in order to use two-way binding, you must implement the INotifyPropertyChanged interface.
In his book he shows a way to wrap up the duplicating code into a base class, ie:
To use this, the following must be done to the entity:
The problem I had with this is the ‘PropertyChangedHandlerâ€™ taking in a string that is to be the property name.
Iâ€™m sure there is a better way to handle this, but I decided to use some static reflection to hand it the property name:
Now, I can use this to replace the above â€œmagic stringâ€: