2 using System.Collections.Generic;
9 namespace System.ComponentModel
12 public static class PropertyChangeEventHandlerExtensions
15 public static void Raise(
this PropertyChangedEventHandler EventHandler, Expression<Func<object>> Expr)
17 if (EventHandler != null)
19 MemberExpression MemberExpr=null;
20 if (Expr.Body is MemberExpression)
22 MemberExpr = Expr.Body as MemberExpression;
24 else if(Expr.Body is UnaryExpression)
26 UnaryExpression UnaryExpr = Expr.Body as UnaryExpression;
27 if (UnaryExpr.Operand is MemberExpression)
29 MemberExpr = UnaryExpr.Operand as MemberExpression;
33 if (MemberExpr == null)
35 throw new Exception(
"Could not raise PropertyChanged event. Could not find the MemberExpression in {0}.".Build(Expr.ToString()));
39 ConstantExpression vmExpression = MemberExpr.Expression as ConstantExpression;
40 if (vmExpression == null)
42 throw new ArgumentException(
"Could not raise PropertyChanged event. Could not find a Constant expression in part {0} of {1}".Build(MemberExpr.ToString(),MemberExpr.Expression.ToString()));
45 LambdaExpression vmlambda = Expression.Lambda(vmExpression);
46 Delegate vmFunc = vmlambda.Compile();
47 object vm = vmFunc.DynamicInvoke();
50 string propertyName = MemberExpr.Member.Name;
51 var e =
new PropertyChangedEventArgs(propertyName);