Search code examples
c#reflectiondynamicexpression-treestypedescriptor

Getting PropertyInfo Name and Value


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:

  1. Be type safe, refactor friendly (no magic strings), and readable
  2. Maximize performance over multiple calls
  3. Provide the name of a property
  4. Provide the value of the property

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
}

Solution

  • 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.