public void Run() { this.commandLine = Console.ReadLine(); while (this.commandLine != null) { var route = new Route(this.commandLine); var controllerType = Assembly.GetExecutingAssembly() .GetTypes() .FirstOrDefault(type => type.Name == route.ControllerName); var controller = Activator .CreateInstance(controllerType, this.database, this.user) as Controller; var action = controllerType.GetMethod(route.ActionName); object[] commandParams = MapParameters(route, action); try { var view = action.Invoke(controller, commandParams) as IView; Console.WriteLine(view.Display()); this.user = controller.User; } catch (Exception ex) { Console.WriteLine(ex.InnerException.Message); } this.commandLine = Console.ReadLine(); } }
private static object[] MapParameters(Route route, MethodInfo action) { // var result = action // .GetParameters() // .Select<ParameterInfo, object>(p => // { // if (p.ParameterType == typeof(int)) // { // return int.Parse(route.Parameters[p.Name]); // } // else // { // return route.Parameters[p.Name]; // } // }).ToArray(); // // return result; var expectedParameters = action.GetParameters(); var argumentsToPass = new List<object>(); foreach (ParameterInfo param in expectedParameters) { var currentArgument = route.Parameters[param.Name]; if (param.ParameterType == typeof (int)) { argumentsToPass.Add(int.Parse(currentArgument)); } else { argumentsToPass.Add(currentArgument); } } return argumentsToPass.ToArray(); }
public void Run() { var database = new BangaloreUniversityDatabase(); User user = null; while (true) { string input = Console.ReadLine(); if (input == null) { break; } var route = new Route(input); var controllerType = Assembly .GetExecutingAssembly() .GetTypes() .FirstOrDefault(type => type.Name == route.ControllerName); var controller = Activator.CreateInstance(controllerType, database, user) as Controller; var action = controllerType.GetMethod(route.ActionName); object[] @params = MapParameters(route, action); try { var view = action.Invoke(controller, @params) as IView; Console.WriteLine(view.Display()); user = controller.User; } catch (Exception ex) { Console.WriteLine(ex.InnerException.Message); } } }
private static object[] MapParameters(Route route, MethodInfo action) { return action.GetParameters().Select<ParameterInfo, object>( p => { if (p.ParameterType == typeof(int)) { return int.Parse(route.Parameters[p.Name]); } return route.Parameters[p.Name]; }).ToArray(); }
public void Run() { User currentUser = null; while (true) { string inputLine = this.reader.ReadNextLine(); if (inputLine == null) { break; } var route = new Route(inputLine); var expectedController = route.ControllerName; var controllerType = Assembly.GetExecutingAssembly() .GetTypes() .FirstOrDefault(type => type.Name == expectedController); var controller = Activator.CreateInstance( controllerType, this.data, currentUser) as Controller; var action = controllerType.GetMethod(route.ActionName); var argumentsToPass = this.MapParameters(route, action); try { var view = action.Invoke(controller, argumentsToPass) as IView; var output = view.Display(); this.writer.Write(output); currentUser = controller.CurrentUser; } catch (Exception ex) { if (ex.InnerException is ArgumentException || ex.InnerException is AuthorizationFailedException) { this.writer.Write(ex.InnerException.Message); } else { throw ex.InnerException; } } } }
public void Run() { User user = null; while (true) { string inputLine = Console.ReadLine(); if (inputLine == null) { break; } var route = new Route(inputLine); var controllerType = Assembly.GetExecutingAssembly() .GetTypes() .FirstOrDefault(type => type.Name == route.ControllerName); var controller = Activator.CreateInstance(controllerType, this.database, user) as ControllerBase; var action = controllerType.GetMethod(route.ActionName); object[] @params = MapParameters(route, action); string viewResult = string.Empty; try { var view = action.Invoke(controller, @params) as IView; viewResult = view.Display(); user = controller.User; } catch (Exception ex) { viewResult = ex.InnerException.Message; } Console.WriteLine(viewResult); } }
public void Run() { var database = new BangaloreUniversityData(); User currentUser = null; while (true) { string routeUrl = Console.ReadLine(); if (routeUrl == null) { break; } var route = new Route(routeUrl); var controllerType = Assembly .GetExecutingAssembly() .GetTypes() .FirstOrDefault(type => type.Name == route.ControllerName); var controller = Activator.CreateInstance(controllerType, database, currentUser) as Controller; var action = controllerType.GetMethod(route.ActionName); object[] parameters = MapParameters(route, action); string viewResult = string.Empty; try { var view = action.Invoke(controller, parameters) as IView; viewResult = view.Display(); currentUser = controller.CurrentUser; } catch (Exception ex) { viewResult = ex.InnerException.Message; } Console.WriteLine(viewResult); } }
private object[] MapParameters(Route route, MethodInfo action) { var expectedMethodParams = action.GetParameters(); var argumentsToPass = new List<object>(); foreach (ParameterInfo param in expectedMethodParams) { var currentArgument = route.Parameters[param.Name]; if (param.ParameterType == typeof(int)) { argumentsToPass.Add(int.Parse(currentArgument)); } else { argumentsToPass.Add(currentArgument); } } return argumentsToPass.ToArray(); }