DCSIMG
PCEH Invoker - Technicals and Technicalities

Technicals and Technicalities

Ariel's uneditable Bliki

PCEH Invoker

(I just love Resharper’s CamelHumps.)

PCEH, if you’re lacking in the Resharper department (and/or don’t use WPF) is PropertyChangedEventHandler, and the simple invoker allows you to write cool code like this:

PropertyChanged.InvokeIfNeededX(sender, () => MyDataBoundProperty);

 

A simple extension method for PropertyChangedEventHandler class, and you get a wonderful syntax for safe invocations. Code below:

 

using System.Linq.Expressions;
namespace System.ComponentModel
{
    public static class PropertyChangedEventHandlerExtensions
    {
        public static void InvokeIfNeededX<T>(this PropertyChangedEventHandler source, object sender, Expression<Func<T>> prop)
        {
            if (source != null)
            {
                source(sender, new PropertyChangedEventArgs(GetName(prop)));
            }
        }
 
        private static string GetName<T>(Expression<Func<T>> expression)
        {
            var member = expression.Body as MemberExpression;
            if (member == null)
            {
                throw new ApplicationException("Cannot handle expression of type " + expression.GetType());
            }
 
            return member.Member.Name;
        }
    }
}

Comments

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# October 12, 2009 12:54 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: