Пример #1
0
        public override object Execute(ControllerContext controllerContext, IDictionary <string, object> parameters)
        {
            if (controllerContext == null)
            {
                throw new ArgumentNullException("controllerContext");
            }
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }

            // Performance sensitive so avoid Linq or delegates.
            ParameterInfo[] parameterInfos  = MethodInfo.GetParameters();
            object[]        parametersArray = new object[parameterInfos.Length];
            for (int i = 0; i < parameterInfos.Length; i++)
            {
                ParameterInfo parameterInfo = parameterInfos[i];
                object        parameter     = ExtractParameterFromDictionary(parameterInfo, parameters, MethodInfo);
                parametersArray[i] = parameter;
            }

            ActionMethodDispatcher dispatcher = DispatcherCache.GetDispatcher(MethodInfo);
            object actionReturnValue          = dispatcher.Execute(controllerContext.Controller, parametersArray);

            return(actionReturnValue);
        }
        public void ExecuteWithStaticActionMethod() {
            // Arrange
            DispatcherController controller = new DispatcherController();
            object[] parameters = new object[0];
            MethodInfo methodInfo = typeof(DispatcherController).GetMethod("StaticAction");
            ActionMethodDispatcher dispatcher = new ActionMethodDispatcher(methodInfo);

            // Act
            object returnValue = dispatcher.Execute(controller, parameters);

            // Assert
            Assert.IsInstanceOfType(returnValue, typeof(int));
            int intResult = (int)returnValue;
            Assert.AreEqual(89, intResult);
        }
        public void ExecuteWithVoidActionMethod() {
            // Arrange
            DispatcherController controller = new DispatcherController();
            object[] parameters = new object[] { 5, "some string", new DateTime(2001, 1, 1) };
            MethodInfo methodInfo = typeof(DispatcherController).GetMethod("VoidAction");
            ActionMethodDispatcher dispatcher = new ActionMethodDispatcher(methodInfo);

            // Act
            object returnValue = dispatcher.Execute(controller, parameters);

            // Assert
            Assert.IsNull(returnValue);
            Assert.AreEqual(5, controller._i);
            Assert.AreEqual("some string", controller._s);
            Assert.AreEqual(new DateTime(2001, 1, 1), controller._dt);
        }
        public void ExecuteWithNormalActionMethod() {
            // Arrange
            DispatcherController controller = new DispatcherController();
            object[] parameters = new object[] { 5, "some string", new DateTime(2001, 1, 1) };
            MethodInfo methodInfo = typeof(DispatcherController).GetMethod("NormalAction");
            ActionMethodDispatcher dispatcher = new ActionMethodDispatcher(methodInfo);

            // Act
            object returnValue = dispatcher.Execute(controller, parameters);

            // Assert
            Assert.IsInstanceOfType(returnValue, typeof(string));
            string stringResult = (string)returnValue;
            Assert.AreEqual("Hello from NormalAction!", stringResult);

            Assert.AreEqual(5, controller._i);
            Assert.AreEqual("some string", controller._s);
            Assert.AreEqual(new DateTime(2001, 1, 1), controller._dt);
        }
Пример #5
0
        public override object Execute(ControllerContext controllerContext, IDictionary <string, object> parameters)
        {
            if (controllerContext == null)
            {
                throw new ArgumentNullException("controllerContext");
            }
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }

            ParameterInfo[] parameterInfos     = MethodInfo.GetParameters();
            var             rawParameterValues = from parameterInfo in parameterInfos
                                                 select ExtractParameterFromDictionary(parameterInfo, parameters, MethodInfo);

            object[] parametersArray = rawParameterValues.ToArray();

            ActionMethodDispatcher dispatcher = DispatcherCache.GetDispatcher(MethodInfo);
            object actionReturnValue          = dispatcher.Execute(controllerContext.Controller, parametersArray);

            return(actionReturnValue);
        }