Пример #1
0
        protected void CreateTargetProxy(object target, BindingDescription description)
        {
            this.DisposeTargetProxy();

            this.targetProxy = this.targetProxyFactory.CreateProxy(target, description);

            if (this.IsSubscribeTargetValueChanged(this.BindingMode) && this.targetProxy is INotifiable)
            {
                this.targetValueChangedHandler = (sender, args) => this.UpdateSourceFromTarget();
                (this.targetProxy as INotifiable).ValueChanged += this.targetValueChangedHandler;
            }
        }
Пример #2
0
        public ITargetProxy CreateProxy(object target, BindingDescription description)
        {
            if (target == null)
            {
                return(null);
            }

            ITargetProxy proxy = null;

            if (TryCreateProxy(target, description, out proxy))
            {
                return(proxy);
            }
            return(proxy);
        }
        public ITargetProxy CreateProxy(object target, BindingDescription description)
        {
            ITargetProxy proxy = null;

            if (TryCreateProxy(target, description, out proxy))
            {
                return(proxy);
            }

            if (log.IsWarnEnabled)
            {
                log.WarnFormat("Unable to bind: not found {0} on {1}", description.TargetName, target.GetType().Name);
            }

            throw new BindingException("Unable to bind: \"{0}\"", description.ToString());
        }
Пример #4
0
 protected void DisposeTargetProxy()
 {
     try
     {
         if (this.targetProxy != null)
         {
             if (this.targetValueChangedHandler != null)
             {
                 (this.targetProxy as INotifiable).ValueChanged -= this.targetValueChangedHandler;
                 this.targetValueChangedHandler = null;
             }
             this.targetProxy.Dispose();
             this.targetProxy = null;
         }
     }
     catch (Exception) { }
 }
Пример #5
0
        public ITargetProxy CreateProxy(object target, BindingDescription description)
        {
            try
            {
                ITargetProxy proxy = null;
                if (TryCreateProxy(target, description, out proxy))
                {
                    return(proxy);
                }

                throw new NotSupportedException("Not found available proxy factory.");
            }
            catch (Exception e)
            {
                throw new ProxyException(e, "Unable to bind the \"{0}\".An exception occurred while creating a proxy for the \"{1}\" property of class \"{2}\".", description.ToString(), description.TargetName, target.GetType().Name);
            }
        }
Пример #6
0
        public ITargetProxy CreateProxy(object target, BindingDescription description)
        {
            try
            {
                ITargetProxy proxy = null;
                if (TryCreateProxy(target, description, out proxy))
                {
                    return(proxy);
                }

                throw new NotSupportedException("Not found available proxy factory.");
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        protected virtual bool TryCreateProxy(object target, BindingDescription description, out ITargetProxy proxy)
        {
            proxy = null;
            foreach (PriorityFactoryPair pair in this.factories)
            {
                try
                {
                    var factory = pair.factory;
                    if (factory == null)
                    {
                        continue;
                    }

                    proxy = factory.CreateProxy(target, description);
                    if (proxy != null)
                    {
                        return(true);
                    }
                }
                catch (Exception e)
                {
                    if (log.IsWarnEnabled)
                    {
                        log.WarnFormat("Unable to bind:{0};exception:{1}", description.ToString(), e);
                    }
                }
            }

            return(false);
        }
        protected virtual ITargetProxy CreateUnityEventProxy(object target, UnityEventBase unityEvent, Type[] paramTypes)
        {
            ITargetProxy proxy = null;

            switch (paramTypes.Length)
            {
            case 0:
                proxy = new UnityEventProxy(target, (UnityEvent)unityEvent);
                break;

            case 1:
#if NETFX_CORE
                TypeCode typeCode = WinRTLegacy.TypeExtensions.GetTypeCode(paramTypes[0]);
#else
                TypeCode typeCode = Type.GetTypeCode(paramTypes[0]);
#endif
                switch (typeCode)
                {
                case TypeCode.String:
                    proxy = new UnityEventProxy <string>(target, (UnityEvent <string>)unityEvent);
                    break;

                case TypeCode.Boolean:
                    proxy = new UnityEventProxy <bool>(target, (UnityEvent <bool>)unityEvent);
                    break;

                case TypeCode.SByte:
                    proxy = new UnityEventProxy <sbyte>(target, (UnityEvent <sbyte>)unityEvent);
                    break;

                case TypeCode.Byte:
                    proxy = new UnityEventProxy <byte>(target, (UnityEvent <byte>)unityEvent);
                    break;

                case TypeCode.Int16:
                    proxy = new UnityEventProxy <short>(target, (UnityEvent <short>)unityEvent);
                    break;

                case TypeCode.UInt16:
                    proxy = new UnityEventProxy <ushort>(target, (UnityEvent <ushort>)unityEvent);
                    break;

                case TypeCode.Int32:
                    proxy = new UnityEventProxy <int>(target, (UnityEvent <int>)unityEvent);
                    break;

                case TypeCode.UInt32:
                    proxy = new UnityEventProxy <uint>(target, (UnityEvent <uint>)unityEvent);
                    break;

                case TypeCode.Int64:
                    proxy = new UnityEventProxy <long>(target, (UnityEvent <long>)unityEvent);
                    break;

                case TypeCode.UInt64:
                    proxy = new UnityEventProxy <ulong>(target, (UnityEvent <ulong>)unityEvent);
                    break;

                case TypeCode.Char:
                    proxy = new UnityEventProxy <char>(target, (UnityEvent <char>)unityEvent);
                    break;

                case TypeCode.Single:
                    proxy = new UnityEventProxy <float>(target, (UnityEvent <float>)unityEvent);
                    break;

                case TypeCode.Double:
                    proxy = new UnityEventProxy <double>(target, (UnityEvent <double>)unityEvent);
                    break;

                case TypeCode.Decimal:
                    proxy = new UnityEventProxy <decimal>(target, (UnityEvent <decimal>)unityEvent);
                    break;

                case TypeCode.DateTime:
                    proxy = new UnityEventProxy <DateTime>(target, (UnityEvent <DateTime>)unityEvent);
                    break;

                case TypeCode.Object:
                default:
#if UNITY_IOS
                    throw new NotSupportedException();
#else
                    proxy = (ITargetProxy)Activator.CreateInstance(typeof(UnityEventProxy <>).MakeGenericType(paramTypes[0]), target, unityEvent);
                    break;
#endif
                }
                break;

            default:
                throw new NotSupportedException("Too many parameters");
            }
            return(proxy);
        }
        //private static readonly ILog log = LogManager.GetLogger(typeof(UnityTargetProxyFactory));

        protected override bool TryCreateProxy(object target, BindingDescription description, out ITargetProxy proxy)
        {
            proxy = null;
            Type       type       = target.GetType();
            MemberInfo memberInfo = type.FindFirstMemberInfo(description.TargetName);

            if (memberInfo == null)
            {
                return(false);
            }

            UnityEventBase updateTrigger = null;

            if (!string.IsNullOrEmpty(description.UpdateTrigger))
            {
                var updateTriggerPropertyInfo = type.GetProperty(description.UpdateTrigger);
                if (updateTriggerPropertyInfo != null)
                {
                    updateTrigger = updateTriggerPropertyInfo.AsProxy().GetValue(target) as UnityEventBase;
                }

                if (updateTriggerPropertyInfo == null)
                {
                    var updateTriggerFieldInfo = type.GetField(description.UpdateTrigger);
                    if (updateTriggerFieldInfo != null)
                    {
                        updateTrigger = updateTriggerFieldInfo.AsProxy().GetValue(target) as UnityEventBase;
                    }
                }
            }

            var propertyInfo = memberInfo as PropertyInfo;

            if (propertyInfo != null)
            {
                if (typeof(IObservableProperty).IsAssignableFrom(propertyInfo.PropertyType))
                {
                    return(false);
                }

                if (typeof(UnityEventBase).IsAssignableFrom(propertyInfo.PropertyType))
                {
                    //Event Type
                    var    proxyPropertyInfo = propertyInfo.AsProxy();
                    object unityEvent        = proxyPropertyInfo.GetValue(target);
                    Type[] paramTypes        = GetUnityEventParametersType(propertyInfo.PropertyType);
                    proxy = CreateUnityEventProxy(target, (UnityEventBase)unityEvent, paramTypes);
                    if (proxy != null)
                    {
                        return(true);
                    }
                    return(false);
                }

                //Other Property Type
                if (updateTrigger == null)/* by UniversalTargetProxyFactory */
                {
                    return(false);
                }

                proxy = CreateUnityPropertyProxy(target, propertyInfo, updateTrigger);
                if (proxy != null)
                {
                    return(true);
                }

                return(false);
            }

            var fieldInfo = memberInfo as FieldInfo;

            if (fieldInfo != null)
            {
                if (typeof(IObservableProperty).IsAssignableFrom(fieldInfo.FieldType))
                {
                    return(false);
                }

                if (typeof(UnityEventBase).IsAssignableFrom(fieldInfo.FieldType))
                {
                    //Event Type
                    var    proxyFieldInfo = fieldInfo.AsProxy();
                    object unityEvent     = proxyFieldInfo.GetValue(target);
                    Type[] paramTypes     = GetUnityEventParametersType(proxyFieldInfo.FieldType);
                    proxy = CreateUnityEventProxy(target, (UnityEventBase)unityEvent, paramTypes);
                    if (proxy != null)
                    {
                        return(true);
                    }
                    return(false);
                }

                //Other Property Type
                if (updateTrigger == null)/* by UniversalTargetProxyFactory */
                {
                    return(false);
                }

                proxy = CreateUnityFieldProxy(target, fieldInfo, updateTrigger);
                if (proxy != null)
                {
                    return(true);
                }

                return(false);
            }

            proxy = null;
            return(false);
        }
Пример #10
0
        //private static readonly ILog log = LogManager.GetLogger(typeof(ObservablePropertyTargetProxyFactory));

        protected override bool TryCreateProxy(object target, BindingDescription description, out ITargetProxy proxy)
        {
            proxy = null;
            Type       type       = target.GetType();
            MemberInfo memberInfo = type.FindFirstMemberInfo(description.TargetName);

            if (memberInfo == null)
            {
                memberInfo = type.FindFirstMemberInfo(description.TargetName, BindingFlags.Instance | BindingFlags.NonPublic);
            }
            if (memberInfo == null)
            {
                return(false);
            }

            var fieldInfo = memberInfo as FieldInfo;

            if (fieldInfo != null && typeof(IObservableProperty).IsAssignableFrom(fieldInfo.FieldType))
            {
                var    fieldType       = fieldInfo.FieldType;
                var    proxyFieldInfo  = fieldInfo.AsProxy();
                object observableValue = proxyFieldInfo.GetValue(target);
                proxy = new ObservablePropertyTargetProxy(target, (IObservableProperty)observableValue);
                return(true);
            }

            var propertyInfo = memberInfo as PropertyInfo;

            if (propertyInfo != null && typeof(IObservableProperty).IsAssignableFrom(propertyInfo.PropertyType))
            {
                var    propertyType      = propertyInfo.PropertyType;
                var    proxyPropertyInfo = propertyInfo.AsProxy();
                object observableValue   = proxyPropertyInfo.GetValue(target);
                proxy = new ObservablePropertyTargetProxy(target, (IObservableProperty)observableValue);
                return(true);
            }

            return(false);
        }
Пример #11
0
 protected abstract bool TryCreateProxy(object target, BindingDescription description, out ITargetProxy proxy);
Пример #12
0
 protected void CreateTargetProxy(object target, BindingDescription bindingDescription)
 {
     this.targetProxy = this.targetProxyFactory.CreateProxy(target, bindingDescription);
 }
        //private static readonly ILog log = LogManager.GetLogger(typeof(UniversalTargetProxyFactory));

        protected override bool TryCreateProxy(object target, BindingDescription description, out ITargetProxy proxy)
        {
            proxy = null;
            Type       type       = target.GetType();
            MemberInfo memberInfo = type.FindFirstMemberInfo(description.TargetName);

            if (memberInfo == null)
            {
                memberInfo = type.FindFirstMemberInfo(description.TargetName, BindingFlags.Instance | BindingFlags.NonPublic);
            }

            if (memberInfo == null)
            {
                return(false);
            }

            var propertyInfo = memberInfo as PropertyInfo;

            if (propertyInfo != null)
            {
                proxy = new PropertyTargetProxy(target, propertyInfo);
                return(true);
            }

            var fieldInfo = memberInfo as FieldInfo;

            if (fieldInfo != null)
            {
                proxy = new FieldTargetProxy(target, fieldInfo);
                return(true);
            }

            var eventInfo = memberInfo as EventInfo;

            if (eventInfo != null)
            {
                proxy = new EventTargetProxy(target, eventInfo);
                return(true);
            }

            var methodInfo = memberInfo as MethodInfo;

            if (methodInfo != null)
            {
                proxy = new VoidMethodTargetProxy(target, methodInfo);
                return(true);
            }

            return(false);
        }
Пример #14
0
        protected virtual bool TryCreateProxy(object target, BindingDescription description, out ITargetProxy proxy)
        {
            proxy = null;
            foreach (PriorityFactoryPair pair in this.factories)
            {
                var factory = pair.factory;
                if (factory == null)
                {
                    continue;
                }

                try
                {
                    proxy = factory.CreateProxy(target, description);
                    if (proxy != null)
                    {
                        return(true);
                    }
                }
                catch (MissingMemberException e)
                {
                    throw e;
                }
                catch (NullReferenceException e)
                {
                    throw e;
                }
                catch (Exception e)
                {
                    if (log.IsWarnEnabled)
                    {
                        log.WarnFormat("An exception occurred when using the \"{0}\" factory to create a proxy for the \"{1}\" property of class \"{2}\";exception:{3}", factory.GetType().Name, description.TargetName, target.GetType().Name, e);
                    }
                }
            }

            return(false);
        }
Пример #15
0
 protected virtual bool TryCreateProxy(object target, BindingDescription description, out ITargetProxy proxy)
 {
     proxy = null;
     return(false);
 }