0
|
1 #region
|
|
2
|
|
3 using System.ComponentModel;
|
|
4 using Castle.DynamicProxy;
|
|
5
|
|
6 #endregion
|
|
7
|
|
8 namespace BLToolkit.Mapping
|
|
9 {
|
|
10 public class NotifyPropertyChangedInterceptor : IInterceptor
|
|
11 {
|
|
12 private readonly string _typeName;
|
|
13 private PropertyChangedEventHandler _subscribers = delegate { };
|
|
14
|
|
15 public NotifyPropertyChangedInterceptor(string typeName)
|
|
16 {
|
|
17 _typeName = typeName;
|
|
18 }
|
|
19
|
|
20 public void Intercept(IInvocation invocation)
|
|
21 {
|
|
22 if (invocation.Method.DeclaringType == typeof (TypeFactory.DataBindingFactory.IMarkerInterface))
|
|
23 {
|
|
24 invocation.ReturnValue = _typeName;
|
|
25 return;
|
|
26 }
|
|
27 if (invocation.Method.DeclaringType == typeof (INotifyPropertyChanged))
|
|
28 {
|
|
29 var propertyChangedEventHandler = (PropertyChangedEventHandler) invocation.Arguments[0];
|
|
30 if (invocation.Method.Name.StartsWith("add_"))
|
|
31 {
|
|
32 _subscribers += propertyChangedEventHandler;
|
|
33 }
|
|
34 else
|
|
35 {
|
|
36 _subscribers -= propertyChangedEventHandler;
|
|
37 }
|
|
38 return;
|
|
39 }
|
|
40 invocation.Proceed();
|
|
41 if (invocation.Method.Name.StartsWith("set_"))
|
|
42 {
|
|
43 var propertyName = invocation.Method.Name.Substring(4);
|
|
44 _subscribers(invocation.InvocationTarget, new PropertyChangedEventArgs(propertyName));
|
|
45 }
|
|
46 }
|
|
47 }
|
|
48 } |