/// <summary> /// Sets the parameter values to a command method. /// </summary> /// <param name="classData"></param> /// <param name="inputData"></param> /// <returns></returns> private static IEnumerable <object> SetParameterValues(CommandClassData classData, CommandInputData inputData) { var parameterValues = new Dictionary <string, object>(); //Get the command method data. var methodData = classData.CommandMethodData.FirstOrDefault(cmd => cmd.MethodName == inputData.MethodName); if (methodData == null) { throw new InputException(string.Format(Constants.ErrorMessages.InvalidCommand, inputData.MethodName)); } if (!methodData.Parameters.Any()) { return(null); } //Set the method parameter default values. methodData.Parameters.ForEach(mp => { parameterValues.Add(mp.Name, mp.DefaultValue); }); //Try to set method parameter values. foreach (var methodParam in methodData.Parameters) { string inputDataValue; if (!inputData.Arguments.TryGetValue(methodParam.Name, out inputDataValue)) { continue; } var paramType = methodParam.ParameterType; var value = (paramType.IsGenericType && paramType.GetGenericTypeDefinition() == typeof(Nullable <>) && string.IsNullOrEmpty(inputDataValue)) ? null : ParseParameterValue(paramType, inputDataValue); parameterValues[methodParam.Name] = value; } return(parameterValues.Values.ToList()); }
/// <summary> /// Reads the existing command class data. /// </summary> /// <param name="cmdNamespace">Command classes namespace.</param> /// <returns>Parsed object reflection information.</returns> public static IEnumerable <CommandClassData> ParseClassData(string cmdNamespace) { try { var result = new List <CommandClassData>(); var cmdClasses = Assembly.GetExecutingAssembly() .GetTypes() .Where(t => t.IsClass && t.Namespace == cmdNamespace) .ToList(); cmdClasses.ForEach(c => { var cmdClassData = new CommandClassData(c.Name); var cmdClassMethods = c.GetMethods(BindingFlags.Static | BindingFlags.Public).ToList(); cmdClassMethods.ForEach(cm => { var requiredValueParams = cm.GetCustomAttribute <ValueRequiredForParamsAttribute>().Parameters; cmdClassData.AddMethodParameterInfos(cm.Name, cm.GetParameters(), requiredValueParams); }); result.Add(cmdClassData); }); return(result); } catch (Exception) { throw new FatalException(Constants.ErrorMessages.FatalError); } }
/// <summary> /// Method to execute the command class method with the user input parameters. /// </summary> /// <param name="classData">Command class data.</param> /// <param name="inputData">User command input data.</param> /// <param name="assembly">Assembly.</param> /// <returns>String representation of the executed method.</returns> public static string ExecuteMethod(CommandClassData classData, CommandInputData inputData, Assembly assembly) { if (classData == null || assembly == null) { throw new FatalException(Constants.ErrorMessages.FatalError); } try { var parameterValues = SetParameterValues(classData, inputData); var classType = assembly.GetType($"{Constants.Commands.CommandNamespace}.{classData.Name}"); object[] methodParameterValues = null; if (parameterValues != null && parameterValues.Any()) { methodParameterValues = parameterValues.ToArray(); } return (classType.InvokeMember(inputData.MethodName, BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.Public, null, null, methodParameterValues).ToString()); } catch (InputException ex) { throw ex; } catch (TargetInvocationException ex) { if (ex.InnerException != null) { throw new InputException(ex.InnerException.Message); } throw new FatalException(ex.Message); } }