Beispiel #1
0
        public ITargetProxy CreateProxy(object target, BindingDescription description)
        {
            IProxyType       type       = target.GetType().AsProxy();
            IProxyMemberInfo memberInfo = type.GetMember(description.TargetName);

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

            if (memberInfo == null)
            {
                throw new MissingMemberException(type.Type.FullName, description.TargetName);
            }

            EventListener updateTrigger = null;

            if (!string.IsNullOrEmpty(description.UpdateTrigger))
            {
                IProxyPropertyInfo updateTriggerPropertyInfo = type.GetProperty(description.UpdateTrigger);
                IProxyFieldInfo    updateTriggerFieldInfo    = updateTriggerPropertyInfo == null?type.GetField(description.UpdateTrigger) : null;

                if (updateTriggerPropertyInfo != null)
                {
                    updateTrigger = updateTriggerPropertyInfo.GetValue(target) as EventListener;
                }

                if (updateTriggerFieldInfo != null)
                {
                    updateTrigger = updateTriggerFieldInfo.GetValue(target) as EventListener;
                }

                if (updateTriggerPropertyInfo == null && updateTriggerFieldInfo == null)
                {
                    throw new MissingMemberException(type.Type.FullName, description.UpdateTrigger);
                }

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

            var propertyInfo = memberInfo as IProxyPropertyInfo;

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

                if (typeof(EventListener).IsAssignableFrom(propertyInfo.ValueType))
                {
                    //Event Type
                    object listener = propertyInfo.GetValue(target);
                    if (listener == null)
                    {
                        throw new NullReferenceException(propertyInfo.Name);
                    }

                    return(new FairyEventProxy(target, (EventListener)listener));
                }

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

                return(new FairyPropertyProxy(target, propertyInfo, updateTrigger));
            }

            var fieldInfo = memberInfo as IProxyFieldInfo;

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

                if (typeof(EventListener).IsAssignableFrom(fieldInfo.ValueType))
                {
                    //Event Type
                    object listener = fieldInfo.GetValue(target);
                    if (listener == null)
                    {
                        throw new NullReferenceException(fieldInfo.Name);
                    }

                    return(new FairyEventProxy(target, (EventListener)listener));
                }

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

                return(new FairyFieldProxy(target, fieldInfo, updateTrigger));
            }

            return(null);
        }
Beispiel #2
0
        //private static readonly ILog log = LogManager.GetLogger(typeof(UnityTargetProxyFactory));

        public ITargetProxy CreateProxy(object target, BindingDescription description)
        {
            IProxyType       type       = target.GetType().AsProxy();
            IProxyMemberInfo memberInfo = type.GetMember(description.TargetName);

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

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

            UnityEventBase updateTrigger = null;

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

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

            var propertyInfo = memberInfo as IProxyPropertyInfo;

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

                if (typeof(UnityEventBase).IsAssignableFrom(propertyInfo.ValueType))
                {
                    //Event Type
                    object unityEvent = propertyInfo.GetValue(target);
                    Type[] paramTypes = GetUnityEventParametersType(propertyInfo.ValueType);
                    return(CreateUnityEventProxy(target, (UnityEventBase)unityEvent, paramTypes));
                }

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

                return(CreateUnityPropertyProxy(target, propertyInfo, updateTrigger));
            }

            var fieldInfo = memberInfo as IProxyFieldInfo;

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

                if (typeof(UnityEventBase).IsAssignableFrom(fieldInfo.ValueType))
                {
                    //Event Type
                    object unityEvent = fieldInfo.GetValue(target);
                    Type[] paramTypes = GetUnityEventParametersType(fieldInfo.ValueType);
                    return(CreateUnityEventProxy(target, (UnityEventBase)unityEvent, paramTypes));
                }

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

                return(CreateUnityFieldProxy(target, fieldInfo, updateTrigger));
            }

            return(null);
        }
Beispiel #3
0
        public ITargetProxy CreateProxy(object target, BindingDescription description)
        {
            IProxyType       type       = target.GetType().AsProxy();
            IProxyMemberInfo memberInfo = type.GetMember(description.TargetName);

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

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

            var propertyInfo = memberInfo as IProxyPropertyInfo;

            if (propertyInfo != null)
            {
                var valueType = propertyInfo.ValueType;
                if (typeof(IObservableProperty).IsAssignableFrom(valueType))
                {
                    object observableValue = propertyInfo.GetValue(target);
                    if (observableValue == null)
                    {
                        return(null);
                    }

                    return(new ObservableTargetProxy(target, (IObservableProperty)observableValue));
                }

                // if (typeof(IInteractionAction).IsAssignableFrom(valueType))
                // {
                //     object interactionAction = propertyInfo.GetValue(target);
                //     if (interactionAction == null)
                //         return null;
                //
                //     return new InteractionTargetProxy(target, (IInteractionAction)interactionAction);
                // }

                return(new PropertyTargetProxy(target, propertyInfo));
            }

            var fieldInfo = memberInfo as IProxyFieldInfo;

            if (fieldInfo != null)
            {
                var valueType = fieldInfo.ValueType;
                if (typeof(IObservableProperty).IsAssignableFrom(valueType))
                {
                    object observableValue = fieldInfo.GetValue(target);
                    if (observableValue == null)
                    {
                        return(null);
                    }

                    return(new ObservableTargetProxy(target, (IObservableProperty)observableValue));
                }

                // if (typeof(IInteractionAction).IsAssignableFrom(valueType))
                // {
                //     object interactionAction = fieldInfo.GetValue(target);
                //     if (interactionAction == null)
                //         return null;
                //
                //     return new InteractionTargetProxy(target, (IInteractionAction)interactionAction);
                // }

                return(new FieldTargetProxy(target, fieldInfo));
            }

            var eventInfo = memberInfo as IProxyEventInfo;

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

            var methodInfo = memberInfo as IProxyMethodInfo;

            if (methodInfo != null)
            {
                return(new MethodTargetProxy(target, methodInfo));
            }

            return(null);
        }
        public ITargetProxy CreateProxy(object target, BindingDescription description)
        {
            if (!target.GetType().IsSubclassOfGenericTypeDefinition(typeof(INotifyValueChanged <>)))
            {
                return(null);
            }

            if ("RegisterValueChangedCallback".Equals(description.TargetName))
            {
                return(this.CreateValueChangedEventProxy(target));
            }

            IProxyType       type       = target.GetType().AsProxy();
            IProxyMemberInfo memberInfo = type.GetMember(description.TargetName);

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

            if (memberInfo == null)
            {
                throw new MissingMemberException(type.Type.FullName, description.TargetName);
            }

            var propertyInfo = memberInfo as IProxyPropertyInfo;

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

                if (typeof(Clickable).IsAssignableFrom(propertyInfo.ValueType))
                {
                    //Event Type
                    object clickable = propertyInfo.GetValue(target);
                    if (clickable == null)
                    {
                        throw new NullReferenceException(propertyInfo.Name);
                    }

                    return(new ClickableEventProxy(target, (Clickable)clickable));
                }

                //Other Property Type
                if (!"RegisterValueChangedCallback".Equals(description.UpdateTrigger))/* by UniversalTargetProxyFactory */
                {
                    return(null);
                }

                return(CreateVisualElementPropertyProxy(target, propertyInfo));
            }

            var fieldInfo = memberInfo as IProxyFieldInfo;

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

                if (typeof(Clickable).IsAssignableFrom(fieldInfo.ValueType))
                {
                    //Event Type
                    object clickable = fieldInfo.GetValue(target);
                    if (clickable == null)
                    {
                        throw new NullReferenceException(fieldInfo.Name);
                    }

                    return(new ClickableEventProxy(target, (Clickable)clickable));
                }

                //Other Property Type
                if (!"RegisterValueChangedCallback".Equals(description.UpdateTrigger))/* by UniversalTargetProxyFactory */
                {
                    return(null);
                }

                return(CreateVisualElementFieldProxy(target, fieldInfo));
            }

            return(null);
        }
Beispiel #5
0
        public ITargetProxy CreateProxy(object target, BindingDescription description)
        {
            IProxyType       type       = target.GetType().AsProxy();
            IProxyMemberInfo memberInfo = type.GetMember(description.TargetName);

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

            if (memberInfo == null)
            {
                throw new MissingMemberException(type.Type.FullName, description.TargetName);
            }

            var propertyInfo = memberInfo as IProxyPropertyInfo;

            if (propertyInfo != null)
            {
                var valueType = propertyInfo.ValueType;
                if (typeof(IObservableProperty).IsAssignableFrom(valueType))
                {
                    object observableValue = propertyInfo.GetValue(target);
                    if (observableValue == null)
                    {
                        throw new NullReferenceException(string.Format("The \"{0}\" property is null in class \"{1}\".", propertyInfo.Name, propertyInfo.DeclaringType.Name));
                    }

                    return(new ObservableTargetProxy(target, (IObservableProperty)observableValue));
                }

                if (typeof(IInteractionAction).IsAssignableFrom(valueType))
                {
                    object interactionAction = propertyInfo.GetValue(target);
                    if (interactionAction == null)
                    {
                        return(null);
                    }

                    return(new InteractionTargetProxy(target, (IInteractionAction)interactionAction));
                }

                return(new PropertyTargetProxy(target, propertyInfo));
            }

            var fieldInfo = memberInfo as IProxyFieldInfo;

            if (fieldInfo != null)
            {
                var valueType = fieldInfo.ValueType;
                if (typeof(IObservableProperty).IsAssignableFrom(valueType))
                {
                    object observableValue = fieldInfo.GetValue(target);
                    if (observableValue == null)
                    {
                        throw new NullReferenceException(string.Format("The \"{0}\" field is null in class \"{1}\".", fieldInfo.Name, fieldInfo.DeclaringType.Name));
                    }

                    return(new ObservableTargetProxy(target, (IObservableProperty)observableValue));
                }

                if (typeof(IInteractionAction).IsAssignableFrom(valueType))
                {
                    object interactionAction = fieldInfo.GetValue(target);
                    if (interactionAction == null)
                    {
                        return(null);
                    }

                    return(new InteractionTargetProxy(target, (IInteractionAction)interactionAction));
                }

                return(new FieldTargetProxy(target, fieldInfo));
            }

            var eventInfo = memberInfo as IProxyEventInfo;

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

            var methodInfo = memberInfo as IProxyMethodInfo;

            if (methodInfo != null)
            {
                return(new MethodTargetProxy(target, methodInfo));
            }

            return(null);
        }
        protected virtual ISourceProxy CreateProxy(object source, IPathNode node)
        {
            Type       type      = GetType(source);
            IProxyType proxyType = type.AsProxy();

            if (node is IndexedNode)
            {
                if (!(source is ICollection))
                {
                    throw new ProxyException("Type \"{0}\" is not a collection and cannot be accessed by index \"{1}\".", proxyType.Type.Name, node.ToString());
                }

                var itemInfo = proxyType.GetItem();
                if (itemInfo == null)
                {
                    throw new MissingMemberException(proxyType.Type.FullName, "Item");
                }

                var intIndexedNode = node as IntegerIndexedNode;
                if (intIndexedNode != null)
                {
                    return(new IntItemNodeProxy((ICollection)source, intIndexedNode.Value, itemInfo));
                }

                var stringIndexedNode = node as StringIndexedNode;
                if (stringIndexedNode != null)
                {
                    return(new StringItemNodeProxy((ICollection)source, stringIndexedNode.Value, itemInfo));
                }

                return(null);
            }

            var memberNode = node as MemberNode;

            if (memberNode == null)
            {
                return(null);
            }

            var memberInfo = proxyType.GetMember(memberNode.Name);

            if (memberInfo == null || memberInfo.IsStatic)
            {
                throw new MissingMemberException(proxyType.Type.FullName, memberNode.Name);
            }

            var proxyPropertyInfo = memberInfo as IProxyPropertyInfo;

            if (proxyPropertyInfo != null)
            {
                var valueType = proxyPropertyInfo.ValueType;
                if (typeof(IObservableProperty).IsAssignableFrom(valueType))
                {
                    object observableValue = proxyPropertyInfo.GetValue(source);
                    if (observableValue == null)
                    {
                        return(null);
                    }

                    return(new ObservableNodeProxy(source, (IObservableProperty)observableValue));
                }
                else if (typeof(IInteractionRequest).IsAssignableFrom(valueType))
                {
                    object request = proxyPropertyInfo.GetValue(source);
                    if (request == null)
                    {
                        return(null);
                    }

                    return(new InteractionNodeProxy(source, (IInteractionRequest)request));
                }
                else
                {
                    return(new PropertyNodeProxy(source, proxyPropertyInfo));
                }
            }

            var proxyFieldInfo = memberInfo as IProxyFieldInfo;

            if (proxyFieldInfo != null)
            {
                var valueType = proxyFieldInfo.ValueType;
                if (typeof(IObservableProperty).IsAssignableFrom(valueType))
                {
                    object observableValue = proxyFieldInfo.GetValue(source);
                    if (observableValue == null)
                    {
                        return(null);
                    }

                    return(new ObservableNodeProxy(source, (IObservableProperty)observableValue));
                }
                else if (typeof(IInteractionRequest).IsAssignableFrom(valueType))
                {
                    object request = proxyFieldInfo.GetValue(source);
                    if (request == null)
                    {
                        return(null);
                    }

                    return(new InteractionNodeProxy(source, (IInteractionRequest)request));
                }
                else
                {
                    return(new FieldNodeProxy(source, proxyFieldInfo));
                }
            }

            var methodInfo = memberInfo as IProxyMethodInfo;

            if (methodInfo != null && methodInfo.ReturnType.Equals(typeof(void)))
            {
                return(new MethodNodeProxy(source, methodInfo));
            }

            var eventInfo = memberInfo as IProxyEventInfo;

            if (eventInfo != null)
            {
                return(new EventNodeProxy(source, eventInfo));
            }

            return(null);
        }