Mercurial > pub > bltoolkit
comparison Extensions/JointureAddOn/Mapping/DataBindingFactory.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 using System; | |
| 2 using System.ComponentModel; | |
| 3 using Castle.DynamicProxy; | |
| 4 | |
| 5 namespace BLToolkit.Mapping | |
| 6 { | |
| 7 public static class DataBindingFactory | |
| 8 { | |
| 9 private static readonly ProxyGenerator ProxyGenerator = new ProxyGenerator(); | |
| 10 | |
| 11 public static T Create<T>() | |
| 12 { | |
| 13 return (T) Create(typeof (T)); | |
| 14 } | |
| 15 | |
| 16 public static object Create(Type type) | |
| 17 { | |
| 18 return ProxyGenerator.CreateClassProxy(type, new[] | |
| 19 { | |
| 20 typeof (INotifyPropertyChanged), | |
| 21 typeof (IMarkerInterface) | |
| 22 }, new NotifyPropertyChangedInterceptor(type.FullName)); | |
| 23 } | |
| 24 | |
| 25 public interface IMarkerInterface | |
| 26 { | |
| 27 string TypeName { get; } | |
| 28 } | |
| 29 | |
| 30 public class NotifyPropertyChangedInterceptor : IInterceptor | |
| 31 { | |
| 32 private readonly string typeName; | |
| 33 private PropertyChangedEventHandler subscribers = delegate { }; | |
| 34 | |
| 35 public NotifyPropertyChangedInterceptor(string typeName) | |
| 36 { | |
| 37 this.typeName = typeName; | |
| 38 } | |
| 39 | |
| 40 public void Intercept(IInvocation invocation) | |
| 41 { | |
| 42 if (invocation.Method.DeclaringType == typeof (IMarkerInterface)) | |
| 43 { | |
| 44 invocation.ReturnValue = typeName; | |
| 45 return; | |
| 46 } | |
| 47 if (invocation.Method.DeclaringType == typeof (INotifyPropertyChanged)) | |
| 48 { | |
| 49 var propertyChangedEventHandler = (PropertyChangedEventHandler) invocation.Arguments[0]; | |
| 50 if (invocation.Method.Name.StartsWith("add_")) | |
| 51 { | |
| 52 subscribers += propertyChangedEventHandler; | |
| 53 } | |
| 54 else | |
| 55 { | |
| 56 subscribers -= propertyChangedEventHandler; | |
| 57 } | |
| 58 return; | |
| 59 } | |
| 60 invocation.Proceed(); | |
| 61 if (invocation.Method.Name.StartsWith("set_")) | |
| 62 { | |
| 63 var propertyName = invocation.Method.Name.Substring(4); | |
| 64 subscribers(invocation.InvocationTarget, new PropertyChangedEventArgs(propertyName)); | |
| 65 } | |
| 66 } | |
| 67 } | |
| 68 } | |
| 69 } |
