A NuGet package. It aim is to help you write extendable and command-based console programs in C# and .Net.
https://www.craigoates.net/Software/Project/7
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
2.9 KiB
60 lines
2.9 KiB
using Console.Waterworks.Assistants; |
|
using Console.Waterworks.Models; |
|
using System; |
|
using System.Collections.Generic; |
|
using System.Linq; |
|
using System.Reflection; |
|
|
|
namespace Console.Waterworks.Specialists |
|
{ |
|
class CommandsSpecialist |
|
{ |
|
CommandsAssistant _assistant = new CommandsAssistant(); |
|
|
|
internal List<Type> GetCommandClasses(string commandsNameSpace) |
|
{ |
|
string theNamespace = commandsNameSpace; |
|
var q = from t in Assembly.GetEntryAssembly().GetTypes() |
|
where t.IsClass && t.Namespace == theNamespace |
|
select t; |
|
return q.ToList(); |
|
} |
|
|
|
internal Dictionary<string, Dictionary<string, IEnumerable<ParameterInfo>>> GetCommandLibraries(List<Type> commandClasses) |
|
{ |
|
var commandLibraries = new Dictionary<string, Dictionary<string, IEnumerable<ParameterInfo>>>(); |
|
foreach (var commandClass in commandClasses) |
|
{ |
|
var methods = commandClass.GetMethods(BindingFlags.Static | BindingFlags.Public); |
|
var methodDictionary = new Dictionary<string, IEnumerable<ParameterInfo>>(); |
|
foreach (var method in methods) |
|
{ |
|
string commandName = method.Name; |
|
methodDictionary.Add(commandName, method.GetParameters()); |
|
} |
|
commandLibraries.Add(commandClass.Name, methodDictionary); |
|
} |
|
return commandLibraries; |
|
} |
|
|
|
internal string ExecuteCommand(string commandsNamespace, Command command, List<Type> commandClasses, Dictionary<string, Dictionary<string, IEnumerable<ParameterInfo>>> commandLibraries, ConsoleIOSpecialist consoleSpec) |
|
{ |
|
var classValidated = _assistant.ValidateClass(command, commandLibraries); |
|
var commandNameValidated = _assistant.ValidateCommand(command, commandLibraries); |
|
Dictionary<string, IEnumerable<ParameterInfo>> methodDictionary; |
|
if (classValidated != true || commandNameValidated != true) |
|
return _assistant.ExecuteBadCommandProcedure(command, consoleSpec); |
|
else |
|
methodDictionary = commandLibraries[command.ClassName]; |
|
var paramInfoList = methodDictionary[command.Name].ToList(); |
|
var paramsValidated = _assistant.ValidateParamArguments(command, paramInfoList); |
|
if (paramsValidated == false) |
|
return _assistant.ExecuteMissingArgumentProcedure(command, paramInfoList, consoleSpec); |
|
var methodParametreValueList = _assistant.GetParametreValueList(command, paramInfoList); |
|
var typeInfo = _assistant.BuildCommandLibraryClass(command, commandsNamespace); |
|
var inputArguments = _assistant.GetInputArguments(methodParametreValueList); |
|
var result = _assistant.InvokeCommand(command, typeInfo, inputArguments); |
|
return result; |
|
} |
|
} |
|
}
|
|
|