I have seen various ways of doing this including reflection, component model type descriptors, expression trees, and aspects, BUT I'm still not sure whether the code below achieves all of the following objectives using .Net 4.0 or higher:
How can the code be improved?
protected void NotifyOfPropertyChanging<TProperty>(Expression<Func<TProperty>> property) {
var memberExpression = (MemberExpression)property.Body;
var prop = (PropertyInfo) memberExpression.Member;
var propertyName = prop.Name;
var value = prop.GetValue(this, null);
// fire INPC using propertyName
// use value and name to do IsDirty checking
}
You might be able to improve performance by compiling the expression tree and caching it for future use rather than calling GetValue
every time. However such optimizations should only be made if your current implementation causes a bottleneck.
void NotifyOfPropertyChanging<TProperty>(Expression<Func<TProperty>> property)
{
var memberExpression = (MemberExpression) property.Body;
var prop = (PropertyInfo) memberExpression.Member;
Func<TProperty> accessor;
if (!TypedAccessorCache<TProperty>.Cache.TryGetValue(prop, out accessor))
{
accessor = property.Compile();
TypedAccessorCache<TProperty>.Cache[prop] = accessor;
}
var value = accessor();
// ...
}
static class TypedAccessorCache<TProperty>
{
public static readonly IDictionary<PropertyInfo, Func<TProperty>> Cache =
new Dictionary<PropertyInfo, Func<TProperty>>();
}
Notice I've used a generic static type to hold the cache dictionary instance. This is a handy way to effectively create a separate typed cache for each distinct property type.