diff Extensions/JointureAddOn/Mapping/NotifyPropertyChangedInterceptor.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Extensions/JointureAddOn/Mapping/NotifyPropertyChangedInterceptor.cs	Thu Mar 27 21:46:09 2014 +0400
@@ -0,0 +1,48 @@
+#region
+
+using System.ComponentModel;
+using Castle.DynamicProxy;
+
+#endregion
+
+namespace BLToolkit.Mapping
+{
+    public class NotifyPropertyChangedInterceptor : IInterceptor
+    {
+        private readonly string _typeName;
+        private PropertyChangedEventHandler _subscribers = delegate { };
+
+        public NotifyPropertyChangedInterceptor(string typeName)
+        {
+            _typeName = typeName;
+        }
+
+        public void Intercept(IInvocation invocation)
+        {
+            if (invocation.Method.DeclaringType == typeof (TypeFactory.DataBindingFactory.IMarkerInterface))
+            {
+                invocation.ReturnValue = _typeName;
+                return;
+            }
+            if (invocation.Method.DeclaringType == typeof (INotifyPropertyChanged))
+            {
+                var propertyChangedEventHandler = (PropertyChangedEventHandler) invocation.Arguments[0];
+                if (invocation.Method.Name.StartsWith("add_"))
+                {
+                    _subscribers += propertyChangedEventHandler;
+                }
+                else
+                {
+                    _subscribers -= propertyChangedEventHandler;
+                }
+                return;
+            }
+            invocation.Proceed();
+            if (invocation.Method.Name.StartsWith("set_"))
+            {
+                var propertyName = invocation.Method.Name.Substring(4);
+                _subscribers(invocation.InvocationTarget, new PropertyChangedEventArgs(propertyName));
+            }
+        }
+    }
+}
\ No newline at end of file