WIP
DirectOutput framework for virtual pinball cabinets WIP
Go to:
Overview 
PropertyChangeEventHandlerExtensions.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
7 
8 
9 namespace System.ComponentModel
10 {
11 
12  public static class PropertyChangeEventHandlerExtensions
13  {
14  //(((propertyExpression as LambdaExpression).Body as UnaryExpression).Operand as MemberExpression).Member.Name
15  public static void Raise(this PropertyChangedEventHandler EventHandler, Expression<Func<object>> Expr)
16  {
17  if (EventHandler != null)
18  {
19  MemberExpression MemberExpr=null;
20  if (Expr.Body is MemberExpression)
21  {
22  MemberExpr = Expr.Body as MemberExpression;
23  }
24  else if(Expr.Body is UnaryExpression)
25  {
26  UnaryExpression UnaryExpr = Expr.Body as UnaryExpression;
27  if (UnaryExpr.Operand is MemberExpression)
28  {
29  MemberExpr = UnaryExpr.Operand as MemberExpression;
30  }
31  }
32 
33  if (MemberExpr == null)
34  {
35  throw new Exception("Could not raise PropertyChanged event. Could not find the MemberExpression in {0}.".Build(Expr.ToString()));
36  }
37 
38  // Extract the right part (after "=&gt;")
39  ConstantExpression vmExpression = MemberExpr.Expression as ConstantExpression;
40  if (vmExpression == null)
41  {
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()));
43  }
44  // Create a reference to the calling object to pass it as the sender
45  LambdaExpression vmlambda = Expression.Lambda(vmExpression);
46  Delegate vmFunc = vmlambda.Compile();
47  object vm = vmFunc.DynamicInvoke();
48 
49  // Extract the name of the property to raise a change on
50  string propertyName = MemberExpr.Member.Name;
51  var e = new PropertyChangedEventArgs(propertyName);
52  EventHandler(vm, e);
53  }
54 
55 
56 
57  }
58  }
59 }