Mercurial > pub > bltoolkit
comparison Extensions/JointureAddOn/Mapping/NotifyPropertyChangedInterceptor.cs @ 0:f990fcb411a9
Копия текущей версии из github
author | cin |
---|---|
date | Thu, 27 Mar 2014 21:46:09 +0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:f990fcb411a9 |
---|---|
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 } |