internal CommandResult Process(string commandText) { if (_commandTypes == null || !_commandTypes.Any()) LoadCommands(); if (string.IsNullOrWhiteSpace(commandText)) throw new ArgumentNullException("commandText", "Command text cannot be empty"); var split = commandText.SplitCommandLine().ToList(); var command = (split.FirstOrDefault() ?? commandText).ToLower(); var subcommand = (split.Count < 2 ? "" : split.Skip(1).Take(1).FirstOrDefault()).ToLower(); var args = (split.Count < 3 ? new string[0] : split.Skip(2)).ToArray(); if (command == "help") return renderHelp(); var commandType = (from c in _commandTypes let attr = c.GetAttribute<WebCommandTypeAttribute>() where attr != null && attr.Name.ToLower() == command select c ).FirstOrDefault(); if(commandType == null) { throw new ArgumentException(string.Format("Command '{0}' not found", command.ToUpper())); } if (split.Count() == 1) { return renderHelp(commandType); } var webCommand = Activator.CreateInstance(commandType); MethodInfo method = _commandMethods.FirstOrDefault( x => (x.GetAttribute<WebCommandAttribute>() != null && (x.GetAttribute<WebCommandAttribute>().Name != null && x.GetAttribute<WebCommandAttribute>().Name.ToLower() == subcommand) || (x.GetAttribute<WebCommandAttribute>().Name == null && x.Name.ToLower() == subcommand)) && x.DeclaringType == commandType); object returnValue; if (method.GetParameters().Count() == 1 && method.GetParameters().Count(x => x.ParameterType == typeof(string[])) == 1) { returnValue = method.Invoke(webCommand, new object[] { args.ToArray() }); } else { if (method.GetParameters().Count() != args.Count()) { StringBuilder builder = new StringBuilder(); foreach (var parameterInfo in method.GetParameters()) { builder.AppendLine(parameterInfo.Name); } returnValue = new CommandResult() { IsError = true, Result = string.Format( "Invalid Number of Parameters Supplied.\r\nParameters that are required are {1}:\r\n{0}", builder, method.GetParameters().Count()) }; } else { object[] objArray = new object[args.Count()]; args.CopyTo(objArray, 0); returnValue = method.Invoke(webCommand, objArray); } } CommandResult result = new CommandResult(); if (returnValue is string) { result = new CommandResult { IsHtml = false, Result = returnValue.ToString() }; } else if (returnValue is CommandResult) { result = returnValue as CommandResult; } return result; }
internal CommandResult Process(string commandText) { if (_commandTypes == null || !_commandTypes.Any()) LoadCommands(); if (string.IsNullOrWhiteSpace(commandText)) throw new ArgumentNullException("commandText", "Command text cannot be empty"); var split = commandText.SplitCommandLine(); var command = (split.FirstOrDefault() ?? commandText).ToLower(); if (command == "help") return renderHelp(); var commandType = (from c in _commandTypes let attr = (WebCommandAttribute)c.GetCustomAttributes(_attributeType, false).FirstOrDefault() where attr != null && attr.Name.ToLower() == command select c).FirstOrDefault(); if(commandType == null) throw new ArgumentException(string.Format("Command '{0}' not found", command.ToUpper())); var webCommand = (IWebCommand)Activator.CreateInstance(commandType); var result = new CommandResult() { IsHtml = webCommand.ReturnHtml }; result.Result = webCommand.Process(split.Skip(1).ToArray()); return result; }
internal CommandResult Process(string commandText) { if (_commandTypes == null || !_commandTypes.Any()) { LoadCommands(); } if (string.IsNullOrWhiteSpace(commandText)) { throw new ArgumentNullException("commandText", "Command text cannot be empty"); } var split = commandText.SplitCommandLine().ToList(); var command = (split.FirstOrDefault() ?? commandText).ToLower(); var subcommand = (split.Count < 2 ? "" : split.Skip(1).Take(1).FirstOrDefault()).ToLower(); var args = (split.Count < 3 ? new string[0] : split.Skip(2)).ToArray(); if (command == "help") { return(renderHelp()); } var commandType = (from c in _commandTypes let attr = c.GetAttribute <WebCommandTypeAttribute>() where attr != null && attr.Name.ToLower() == command select c ).FirstOrDefault(); if (commandType == null) { throw new ArgumentException(string.Format("Command '{0}' not found", command.ToUpper())); } if (split.Count() == 1) { return(renderHelp(commandType)); } var webCommand = Activator.CreateInstance(commandType); MethodInfo method = _commandMethods.FirstOrDefault( x => (x.GetAttribute <WebCommandAttribute>() != null && (x.GetAttribute <WebCommandAttribute>().Name != null && x.GetAttribute <WebCommandAttribute>().Name.ToLower() == subcommand) || (x.GetAttribute <WebCommandAttribute>().Name == null && x.Name.ToLower() == subcommand)) && x.DeclaringType == commandType); object returnValue; if (method.GetParameters().Count() == 1 && method.GetParameters().Count(x => x.ParameterType == typeof(string[])) == 1) { returnValue = method.Invoke(webCommand, new object[] { args.ToArray() }); } else { if (method.GetParameters().Count() != args.Count()) { StringBuilder builder = new StringBuilder(); foreach (var parameterInfo in method.GetParameters()) { builder.AppendLine(parameterInfo.Name); } returnValue = new CommandResult() { IsError = true, Result = string.Format( "Invalid Number of Parameters Supplied.\r\nParameters that are required are {1}:\r\n{0}", builder, method.GetParameters().Count()) }; } else { object[] objArray = new object[args.Count()]; args.CopyTo(objArray, 0); returnValue = method.Invoke(webCommand, objArray); } } CommandResult result = new CommandResult(); if (returnValue is string) { result = new CommandResult { IsHtml = false, Result = returnValue.ToString() }; } else if (returnValue is CommandResult) { result = returnValue as CommandResult; } return(result); }