public CliControllerInstance CreateController(Type controller, ICommandLineArguments commandLineArgs) { object instance = Activator.CreateInstance(controller); var controllerInstance = new CliControllerInstance(controller, instance); SatisfyProperties(controllerInstance, commandLineArgs); return(controllerInstance); }
private void SatisfyProperty(CliControllerInstance controller, ICommandLineArguments commandLineArgs, PropertyInfo property) { CliSwitchAttribute switchAttribute = property.GetCustomAttribute <CliSwitchAttribute>(); if (switchAttribute != null) { bool switchExists = false; switchExists |= commandLineArgs.GetSwitch(switchAttribute.ShortName); switchExists |= commandLineArgs.GetSwitch(switchAttribute.LongName); property.SetValue(controller.Instance, switchExists); } }
private void SatisfyProperties(CliControllerInstance controller, ICommandLineArguments commandLineArgs) { const BindingFlags bindingFlags = BindingFlags.GetProperty | BindingFlags.SetProperty | BindingFlags.Public | BindingFlags.Instance; PropertyInfo[] properties = controller.Type.GetTypeInfo() .GetProperties(bindingFlags); foreach (PropertyInfo property in properties) { SatisfyProperty(controller, commandLineArgs, property); } }
public int Run(string[] args) { var routeFinder = new CliRouteFinder(_controllerHost, _cliOptions); ICliAction cliAction = routeFinder.Match(args); if (cliAction == null) { return(0); } else { MethodInfo mi = cliAction.Method; ParameterInfo[] miParameters = mi.GetParameters(); ICommandLineArguments commandLineArgs = new CommandLineArguments(); var controllerFactory = new CliControllerFactory(); CliControllerInstance controllerInstance = controllerFactory.CreateController(cliAction.Controller, commandLineArgs); object[] methodArgs = new object[miParameters.Length]; object result = mi.Invoke(controllerInstance.Instance, methodArgs); if (result is bool) { return((bool)result ? 0 : 1); } else { try { return(Convert.ToInt32(result)); } catch { return(0); } } } }