Information Technology Reference
In-Depth Information
if (postHandler != null )
postHandler(target, new
PropertyChangedEventArgs (propName));
}
return newValue;
}
}
Before I go through all the code, let me begin with a simple disclaimer. I
removed some of the error handling for space. In production code, you'd
need to check that the casts worked, and that the property setter was found.
Yo u ' d a l s o n e e d t o h a n d l e p o s s i b l e s e c u r i t y e x c e p t i o n s i n a S i l v e r l i g h t
sandbox.
The first course of action is to compile and execute the property get
expression and compare that value to the new value. There's no reason to
do any work if the old and new values are the same. Just compile the
expression and execute it.
The next part is more complicated. This code parses the expression to find
the important components needed to set the value and to raise the
INotifyPropertyChanging and INotifyPropertyChanged events. That
means finding the name of the property, the type of the target object, and
accessing the property setter. Remember how this method was called.
Here's the expression that maps to the oldValueExpression:
() => UsedMemory
That's a member access expression. The member expression contains the
Member property, which is the PropertyInfo for the property being
changed. One of its members is the Name of the property, which is where
you get the string “UsedMemory”, which you'll need to raise the event.
The PropertyInfo object has another use for you: You'll use Reflection APIs
on the PropertyInfo object to change the value of the property.
The technique here can be applied to other problems as well where the
framework requires string information on methods or properties. In fact,
LINQ to SQL and the Entity Framework are built on the System.Linq
.Expression APIs. Those APIs allow you to treat code as data. You can exam-
ine the code using the Expression APIs. You can change algorithms, create
new code, and execute the code. It's a great way to build dynamic systems.
DataBinding, by its very nature, requires that you work with the string
representation of your properties. INotifyPropertyChanged, and INotify-
 
Search WWH ::




Custom Search