Example #1
0
        //tries to get a CommandBehaviorBinding from the element. Creates a new instance if there is not one attached
        private static CommandBehaviorBinding FetchOrCreateBinding(DependencyObject d)
        {
            CommandBehaviorBinding binding = GetBehavior(d);

            if (binding == null)
            {
                binding = new CommandBehaviorBinding();
                SetBehavior(d, binding);
            }

            return(binding);
        }
Example #2
0
        /// <summary>
        /// Главная точка входа
        /// </summary>
        private static void OnEventChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            CommandBehaviorBinding binding = FetchOrCreateBinding(d);

            //	check if the Event is set. If yes we need to rebind the Command to the new event and unregister the old one
            if (binding.Event != null && binding.Owner != null)
            {
                binding.Dispose();
            }
            //bind the new event to the command
            binding.BindEvent(d, e.NewValue.ToString());
        }
Example #3
0
            //Создает EventHandler во время выполнения и регистрирует обработчик указанного события
            public void BindEvent(DependencyObject owner, string eventName)
            {
                this.EventName = eventName;
                this.Owner     = owner;
                this.Event     = Owner.GetType().GetEvent(this.EventName, BindingFlags.Public | BindingFlags.Instance);
                if (this.Event == null)
                {
                    throw new InvalidOperationException(String.Format("Не удалось разрешить имя события {0}", EventName));
                }

                //	Создается обработчик события для event, который вызовет ExecuteCommand()
                var mi = typeof(CommandBehaviorBinding).GetMethod("ExecuteCommand2", BindingFlags.Public | BindingFlags.Instance);

                this.EventHandler = CommandBehaviorBinding.CreateDelegate(this.Event.EventHandlerType, mi, this);

                //	Регистрация нашего обработчика для Event
                this.Event.AddEventHandler(this.Owner, this.EventHandler);
                this.disposed = false;
            }
Example #4
0
        /// <summary>
        /// Handles changes to the Event property.
        /// </summary>
        private static void OnEventChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(d))
            {
                return;
            }

            CommandBehaviorBinding binding = FetchOrCreateBinding(d);

            //check if the Event is set. If yes we need to rebind the Command to the new event and unregister the old one
            if (binding.Event != null && binding.Owner != null)
            {
                binding.Dispose();
            }

            //if (string.IsNullOrEmpty((string)e.NewValue))

            //    return;

            //bind the new event to the command
            binding.BindEvent(d, e.NewValue.ToString());
        }
Example #5
0
        /// <summary>
        /// 过滤器的方式执行 Action
        /// </summary>
        /// <param name="action">Controller中的Action</param>
        /// <param name="parameter">Controller中的Action的参数</param>
        /// <param name="Route">Mvc路由信息模型,无需设置Action和ActionName,如果你在filter中需要信息,你可以传递此参数,主要是EventName,Owner值设置</param>
        public void UseFilter(Expression <Func <T, ActionResult> > action, object parameter, CommandBehaviorBinding Route)
        {
            Func <T, ActionResult> result        = action.Compile();
            ActionResult           _actionResult = result(Controller);

            if (_actionResult != null)
            {
                var memberExpression = action.Body as MemberExpression;
                if (memberExpression == null)
                {
                    throw new ArgumentException("PropertySupport_NotMemberAccessExpression_Exception", "propertyExpression");
                }

                var propertyInfo = memberExpression.Member as System.Reflection.PropertyInfo;
                if (propertyInfo == null)
                {
                    throw new ArgumentException("PropertySupport_ExpressionNotProperty_Exception", "propertyExpression");
                }

                //获取控制器类型

                var objTyp1 = Controller.GetType();
                var objType = Controller.GetType().DeclaringType;
                if (objType == null && objTyp1 != null)
                {
                    objType = objTyp1;
                }
                if (objType == null && objTyp1 == null)
                {
                    objType = Route.Action.Target.GetType();
                    if (objType == null)
                    {
                        return;
                    }
                }
                if (objType != null)
                {
                    //获得控制器是否有filter AY 2017-8-17 14:06:01
                    var controllerAttribute =
                        objType.GetCustomAttributes(typeof(FilterAttribute), false);

                    List <FilterAttribute> controllerFilters = null;
                    List <FilterAttribute> actionFilters     = null;

                    if (controllerAttribute != null && controllerAttribute.Length > 0)
                    {
                        controllerFilters = new List <FilterAttribute>();
                        foreach (object ta in controllerAttribute)
                        {
                            controllerFilters.Add(ta as FilterAttribute);
                        }
                        //排序筛选器 AY 2017-8-17 14:05:56
                        //同类型过滤器 controller和action只会执行1次,或者2个都执行
                        controllerFilters = controllerFilters.OrderBy(a => a.Order).ToList();
                    }



                    if (propertyInfo != null)
                    {
                        var attribute =
                            propertyInfo.GetCustomAttributes(typeof(FilterAttribute), false);

                        if (attribute != null && attribute.Length > 0)
                        {
                            actionFilters = new List <FilterAttribute>();
                            foreach (object ta in attribute)
                            {
                                actionFilters.Add(ta as FilterAttribute);
                            }
                        }
                    }


                    var _this = new ActionExecutionStrategy();
                    if (Route == null)
                    {
                        Route = new CommandBehaviorBinding();
                    }
                    Route.Action           = _actionResult;
                    Route.ActionName       = propertyInfo.Name;
                    Route.CommandParameter = parameter;

                    //设置controller 2017-9-21 13:35:36
                    if (Route.Owner != null)
                    {
                        System.Windows.FrameworkElement _100 = Route.Owner as System.Windows.FrameworkElement;
                        if (_100 != null)
                        {
                            Route.Controller = _100.DataContext as Controller;
                            if (Controller == null)
                            {
                                var _1011 = _100.GetVisualAncestor <System.Windows.Controls.UserControl>();
                                if (_1011 != null)
                                {
                                    Route.Controller = _1011.DataContext as Controller;
                                    if (Controller == null)
                                    {
                                        var _101 = _100.GetVisualAncestor <System.Windows.Controls.Page>();
                                        if (_101 != null)
                                        {
                                            Route.Controller = _101.DataContext as Controller;
                                        }

                                        if (Controller == null)
                                        {
                                            var _102 = System.Windows.Window.GetWindow(_100);
                                            if (_102 != null)
                                            {
                                                Route.Controller = _102.DataContext as Controller;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }


                    var _testIsArray = parameter as object[];
                    if (_testIsArray != null)
                    {
                        Route.IsSendEventArgs = true;
                    }
                    _this.Route = Route;


                    if (controllerFilters == null && actionFilters == null)
                    {
                        Route.Action(parameter);
                    }
                    else if (controllerFilters == null && actionFilters != null)
                    {
                        bool isForbid = false;
                        List <AuthorizeAttribute>    authorizationFilter = new List <AuthorizeAttribute>();
                        List <ActionFilterAttribute> actionFilter        = new List <ActionFilterAttribute>();
                        for (int i = 0; i < actionFilters.Count(); i++)
                        {
                            var _temp = actionFilters[i] as AuthorizeAttribute;
                            if (_temp != null)
                            {
                                authorizationFilter.Add(_temp);
                            }
                            else
                            {
                                var _temp2 = actionFilters[i] as ActionFilterAttribute;
                                if (_temp2 != null)
                                {
                                    actionFilter.Add(_temp2);
                                }
                            }
                        }
                        authorizationFilter = authorizationFilter.OrderBy(a => a.Order).ToList();
                        //优先执行IAuthorizationFilter
                        foreach (var item in authorizationFilter)
                        {
                            bool isOK = item.OnAuthorization(_this);
                            if (!isOK)
                            {
                                isForbid = true;
                                break;
                            }
                        }
                        if (isForbid)
                        {
                            return;
                        }
                        else
                        {
                            actionFilter = actionFilter.OrderBy(a => a.Order).ToList();
                            foreach (var item in actionFilter)
                            {
                                item.OnActionExecuting(_this);
                            }
                            _actionResult(parameter);

                            //2017-8-18 11:58:45  从 Controller级别执行前-> action级别执行前 -> 执行  -> action级别执行后 ->  Controller级别 执行后
                            int maxLength = actionFilter.Count();
                            for (int i = maxLength - 1; i >= 0; i--)
                            {
                                actionFilter[i].OnActionExecuted(_this);
                            }
                        }
                    }
                    else if (controllerFilters != null && actionFilters == null)  //控制器全局
                    {
                        bool isForbid = false;
                        List <AuthorizeAttribute>    authorizationFilter = new List <AuthorizeAttribute>();
                        List <ActionFilterAttribute> actionFilter        = new List <ActionFilterAttribute>();
                        for (int i = 0; i < controllerFilters.Count(); i++)
                        {
                            var _temp = controllerFilters[i] as AuthorizeAttribute;
                            if (_temp != null)
                            {
                                authorizationFilter.Add(_temp);
                            }
                            else
                            {
                                var _temp2 = controllerFilters[i] as ActionFilterAttribute;
                                if (_temp2 != null)
                                {
                                    actionFilter.Add(_temp2);
                                }
                            }
                        }
                        authorizationFilter = authorizationFilter.OrderBy(a => a.Order).ToList();
                        //优先执行IAuthorizationFilter
                        foreach (var item in authorizationFilter)
                        {
                            bool isOK = item.OnAuthorization(_this);
                            if (!isOK)
                            {
                                isForbid = true;
                                break;
                            }
                        }
                        if (isForbid)
                        {
                            return;
                        }
                        else
                        {
                            actionFilter = actionFilter.OrderBy(a => a.Order).ToList();
                            foreach (var item in actionFilter)
                            {
                                item.OnActionExecuting(_this);
                            }
                            Route.Action(parameter);

                            //2017-8-18 11:58:45  从 Controller级别执行前-> action级别执行前 -> 执行  -> action级别执行后 ->  Controller级别 执行后
                            int maxLength = actionFilter.Count();
                            for (int i = maxLength - 1; i >= 0; i--)
                            {
                                actionFilter[i].OnActionExecuted(_this);
                            }
                        }
                    }
                    else if (controllerFilters != null && actionFilters != null)
                    {
                        //第一步洗牌:排序
                        //第二步洗牌:归类
                        //第三步洗牌:筛选
                        //第四步洗牌:挑拣
                        //第五步洗牌:为了执行
                        bool isForbid = false;
                        //归类
                        List <AuthorizeAttribute>    authorizationControllerFilter = new List <AuthorizeAttribute>();
                        List <ActionFilterAttribute> actionControllerFilter        = new List <ActionFilterAttribute>();
                        for (int i = 0; i < controllerFilters.Count(); i++)
                        {
                            var _temp = controllerFilters[i] as AuthorizeAttribute;
                            if (_temp != null)
                            {
                                authorizationControllerFilter.Add(_temp);
                            }
                            else
                            {
                                var _temp2 = controllerFilters[i] as ActionFilterAttribute;
                                if (_temp2 != null)
                                {
                                    actionControllerFilter.Add(_temp2);
                                }
                            }
                        }
                        //当前要被执行的的过滤器
                        List <AuthorizeAttribute>    authorizationFilterWilling = new List <AuthorizeAttribute>();
                        List <ActionFilterAttribute> actionFilterWilling        = new List <ActionFilterAttribute>();
                        for (int i = 0; i < actionFilters.Count(); i++)
                        {
                            var _temp = actionFilters[i] as AuthorizeAttribute;
                            if (_temp != null)
                            {
                                var  _afilterType = _temp.GetType();
                                bool hasSame      = false;
                                foreach (var item in authorizationControllerFilter)
                                {
                                    if (_afilterType == item.GetType())
                                    {
                                        hasSame = true;
                                        //查看策略
                                        switch (_temp.FilterScope)
                                        {
                                        case FilterScope.Controller:
                                            authorizationFilterWilling.Add(item);
                                            break;

                                        case FilterScope.Action:
                                            authorizationFilterWilling.Add(_temp);
                                            break;

                                        case FilterScope.Both:
                                            authorizationFilterWilling.Add(item);
                                            authorizationFilterWilling.Add(_temp);
                                            break;
                                        }
                                        break;
                                    }
                                }
                                if (!hasSame)
                                {
                                    authorizationFilterWilling.Add(_temp);
                                }
                            }
                            else
                            {
                                var _temp2 = actionFilters[i] as ActionFilterAttribute;
                                if (_temp2 != null)
                                {
                                    var  _afilterType = _temp2.GetType();
                                    bool hasSame      = false;
                                    foreach (var item in actionControllerFilter)
                                    {
                                        if (_afilterType == item.GetType())
                                        {
                                            hasSame = true;
                                            //查看策略
                                            switch (_temp2.FilterScope)
                                            {
                                            case FilterScope.Controller:
                                                actionFilterWilling.Add(item);
                                                break;

                                            case FilterScope.Action:
                                                actionFilterWilling.Add(_temp2);
                                                break;

                                            case FilterScope.Both:
                                                actionFilterWilling.Add(item);
                                                actionFilterWilling.Add(_temp2);
                                                break;
                                            }
                                            break;
                                        }
                                    }
                                    if (!hasSame)
                                    {
                                        actionFilterWilling.Add(_temp2);
                                    }
                                }
                            }
                        }

                        //执行时,action是同类别的filter,如果有,获得FilterScope,决定再怎么执行
                        //优先执行授权,再action
                        //优先执行IAuthorizationFilter  ============筛选AuthorizeAttribute
                        //第四步 挑拣 ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ AY2017-8-18 11:41:01
                        int aclength = authorizationControllerFilter.Count();
                        int aalength = authorizationFilterWilling.Count();
                        for (int i = 0; i < aclength; i++)
                        {
                            bool hasSame = false;
                            for (int j = 0; j < aalength; j++)
                            {
                                if (authorizationControllerFilter[i].GetType() == authorizationFilterWilling[j].GetType())
                                {
                                    hasSame = true;
                                    break;
                                }
                            }
                            if (!hasSame)
                            {
                                authorizationFilterWilling.Add(authorizationControllerFilter[i]);
                            }
                        }


                        int alength  = actionControllerFilter.Count();
                        int awlength = actionFilterWilling.Count();
                        for (int i = 0; i < aclength; i++)
                        {
                            bool hasSame = false;
                            for (int j = 0; j < awlength; j++)
                            {
                                if (actionControllerFilter[i].GetType() == actionFilterWilling[j].GetType())
                                {
                                    hasSame = true;
                                    break;
                                }
                            }
                            if (!hasSame)
                            {
                                actionFilterWilling.Add(actionControllerFilter[i]);
                            }
                        }

                        //第四步 挑拣完成↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
                        //优先执行IAuthorizationFilter
                        authorizationFilterWilling = authorizationFilterWilling.OrderBy(a => a.Order).ToList();
                        foreach (var item in authorizationFilterWilling)
                        {
                            bool isOK = item.OnAuthorization(_this);
                            if (!isOK)
                            {
                                isForbid = true;
                                break;
                            }
                        }
                        if (isForbid)
                        {
                            return;
                        }
                        else
                        {
                            actionFilterWilling = actionFilterWilling.OrderBy(a => a.Order).ToList();
                            foreach (var item in actionFilterWilling)
                            {
                                item.OnActionExecuting(_this);
                            }
                            Route.Action(parameter);
                            //2017-8-18 11:58:45  从 Controller级别执行前-> action级别执行前 -> 执行  -> action级别执行后 ->  Controller级别 执行后
                            int maxLength = actionFilterWilling.Count();
                            for (int i = maxLength - 1; i >= 0; i--)
                            {
                                actionFilterWilling[i].OnActionExecuted(_this);
                            }
                        }
                    }
                }
            }



            //var getMethod = property.GetGetMethod(true);
            //if (getMethod.IsStatic)
            //{
            //    throw new ArgumentException("PropertySupport_StaticExpression_Exception", "propertyExpression");
            //}
        }
Example #6
0
 /// <summary>
 /// 过滤器的方式执行 Action
 /// </summary>
 /// <param name="action">Controller中的Action</param>
 /// <param name="parameter">Controller中的Action的参数</param>
 /// <param name="sender">当前事件的Sender参数</param>
 /// <param name="eventArgs">当前事件的args参数</param>
 /// <param name="Route">Mvc路由信息模型,无需设置Action和ActionName,如果你在filter中需要信息,你可以传递此参数,主要是EventName,Owner值设置</param>
 public void UseFilter(Expression <Func <T, ActionResult> > action, object parameter, object sender, EventArgs eventArgs, CommandBehaviorBinding Route)
 {
     UseFilter(action, new object[] { parameter, sender, eventArgs }, Route);
 }
Example #7
0
 /// <summary>
 /// Sets the Behavior property.
 /// </summary>
 private static void SetBehavior(DependencyObject d, CommandBehaviorBinding value) => d.SetValue(BehaviorProperty, value);
Example #8
0
        /// <summary>
        /// Одна из точек входа
        /// </summary>
        private static void OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            CommandBehaviorBinding binding = FetchOrCreateBinding(d);

            binding.Command = (ICommand)e.NewValue;
        }
 /// <summary>
 /// Sets the Behavior property.  
 /// </summary>
 private static void SetBehavior(DependencyObject d, CommandBehaviorBinding value)
 {
     d.SetValue(BehaviorProperty, value);
 }
 //tries to get a CommandBehaviorBinding from the element. Creates a new instance if there is not one attached
 private static CommandBehaviorBinding FetchOrCreateBinding(DependencyObject d)
 {
     CommandBehaviorBinding binding = CommandBehavior.GetBehavior(d);
     if (binding == null)
     {
         binding = new CommandBehaviorBinding();
         CommandBehavior.SetBehavior(d, binding);
     }
     return binding;
 }
Example #11
0
 /// <summary>Sets the Behavior property</summary>
 private static void Set__Behavior([NotNull] DependencyObject d, CommandBehaviorBinding value) => d.SetValue(__BehaviorProperty, value);