static bool ValidateParameters(MethodDetails md, int argumentCount) { int requiredCount = md.MethodParameters.Where(p => p.IsOptional == false).Count(); int optionalCount = md.MethodParameters.Where(p => p.IsOptional == true).Count(); int providedCount = argumentCount; if (requiredCount > providedCount) { Console.WriteLine("Failed to provide the correct number of required arguments."); Console.WriteLine("You provided {0} arguments, there are {1} required arguments, and {2} optional arguments." + Environment.NewLine, providedCount, requiredCount, optionalCount); // Display Help Text for the Method md.DisplayHelpText(); return(false); } else if (providedCount > requiredCount) { Console.WriteLine("Provided too many arguments. When using a string make sure to wrap it in quotes if it has spaces so it will be treated as a single argument"); Console.WriteLine("You provided {0} arguments, there are {1} required arguments, and {2} optional arguments." + Environment.NewLine, providedCount, requiredCount, optionalCount); // Display Help Text for the Method md.DisplayHelpText(); return(false); } return(true); }
static void InvokeMethod(MethodDetails method, object[] paramArray) { try { method.Method.Invoke(_this, paramArray); } catch (TargetInvocationException ex) { throw ex.InnerException; } }
static void ExecuteCommand(HostCommand cmd) { try { // Validate The Passed in Command if (!ValidateCommand(cmd)) { Console.WriteLine("{0} is an invalid command", cmd.MethodName); Help(); return; } // Make sure the user provided the correct number of arguments MethodDetails methodDetails = _methodDictionary[cmd.MethodName]; if (!ValidateParameters(methodDetails, cmd.Arguments.Count())) { return; } object[] inputs = GetMethodParams(methodDetails, cmd.Arguments); InvokeMethod(methodDetails, inputs); } catch (Exception e) { Console.WriteLine(); WriteExceptions(e); if (e.InnerException != null) { Console.WriteLine(); WriteExceptions(e.InnerException); } } }
static object[] GetMethodParams(MethodDetails method, IEnumerable <string> args) { var methodParams = new List <object>(); if (method.MethodParameters.Count() > 0) { foreach (var param in method.MethodParameters) { methodParams.Add(param.DefaultValue); } if (args.Count() == 0) { return(methodParams.ToArray()); } if (method.MethodParameters.HasParameterType <List <string> >()) { List <string> arguments = new List <string>(); for (int i = 0; i < args.Count(); ++i) { var arg = args.ElementAt(i); if (args.ElementAt(i).Contains(",")) { var s = arg.Replace(",", ""); if (s != string.Empty) { arguments.Add(s); } } else { arguments.Add(arg); } } args = new List <string>() { string.Join(",", arguments.ToArray()) }; } for (int i = 0; i < args.Count(); ++i) { var methodParam = method.MethodParameters.ElementAt(i); var requiredType = methodParam.ParameterType; object value = null; try { MethodInfo changeTypeMethod = null; if (!requiredType.IsGenericType) { changeTypeMethod = typeof(HostExtensions).GetMethod("ChangeType"); } else { changeTypeMethod = typeof(HostExtensions).GetMethod("ChangeTypeList"); } var genericMethod = changeTypeMethod.MakeGenericMethod(requiredType); value = genericMethod.Invoke(null, new object[] { args.ElementAt(i), CultureInfo.CurrentCulture }); methodParams[i] = value; } catch (Exception ex) { throw new ArgumentException(string.Format("The value for '{0}' cannot be parsed to '{1}' ", methodParam.Name, requiredType.Name), ex); } } } return(methodParams.ToArray()); }