static void ExecuteCommand(string input, NHTSAClient client) { string[] inputArray = input.Split(' '); string command = string.Empty; List <KeyValuePair <string, string> > commandParameters = new List <KeyValuePair <string, string> >(); //parse command from input command = inputArray.ElementAtOrDefault(0); if (string.IsNullOrEmpty(command)) { Art.PrintError("Invalid or missing command"); return; } //parse command parameters if (inputArray.Length > 1) { foreach (var item in inputArray.Skip(1)) { string[] paramsArray = item.Split('='); commandParameters.Add(new KeyValuePair <string, string>(paramsArray.ElementAtOrDefault(0), paramsArray.ElementAtOrDefault(1))); } } Type clientType = typeof(NHTSAClient); MethodInfo mi = clientType.GetMethods() .FirstOrDefault(t => t.GetCustomAttribute <FunctionInfoAttribute>() != null && t.GetCustomAttribute <FunctionInfoAttribute>().Name.Equals(command)); if (mi == null) { Art.PrintError($"{command} is an invalid command"); } ParameterInfo[] pInfos = mi.GetParameters(); //Only supports string parameters at current. Dont think i will need anything else here. object[] funcParameters = new object[pInfos.Length]; int fIndex = 0; foreach (var item in pInfos) { var cParam = commandParameters.Where(t => t.Key.Equals(item.Name, StringComparison.CurrentCultureIgnoreCase)); if (cParam.Count() == 1) { funcParameters[fIndex] = ParameterConverter.Convert(cParam.Single().Value, item.ParameterType); fIndex++; } } var result = mi.Invoke(client, funcParameters); if (result != null) { string jsonResult = JsonConvert.SerializeObject(result, Formatting.Indented); Console.BackgroundColor = ConsoleColor.White; Console.ForegroundColor = ConsoleColor.DarkGray; Console.WriteLine(jsonResult); Console.ResetColor(); } }
private static object ConvertToType(Type type, object expected) { //TODO: break this out or use TypeConverters return(_parameterConverter.Convert(expected.ToString(), type)); }
private object ConvertParam(string s, Type t) { return(_parameterConverter.Convert(s, t)); }