/// <summary> /// 显示命令操作说明 /// </summary> public void ShowCommand() { if (JobTypeMap == null || JobTypeMap.Count() <= 0) { ConsoleHelper.WriteLineByColor("没有可执行的命令!!!", ConsoleColor.Red); return; } Console.ForegroundColor = ConsoleColor.Green; ConsoleHelper.DrawHalfSplitLine(); Console.WriteLine("操作命令说明(输入命令名称以执行任务,区分大小写):\n"); /* * 使用ConsoleTable类库绘制表格 * https://github.com/khalidabuhakmeh/ConsoleTables */ ConsoleTable table = new ConsoleTable("No.", "CommandName", "Description"); int counter = 0; foreach (var c in CommandInfoList) { table.AddRow(counter, c.Key, c.Value); counter++; } table.Write(); ConsoleHelper.DrawHalfSplitLine(); Console.ResetColor(); }
/// <summary> /// 执行命令 /// </summary> /// <param name="input"></param> public void ExecuteCommand(string input) { if (input == "exit") { IsRun = false; } else if (input == "help") { ShowCommand(); } else if (JobTypeMap.ContainsKey(input)) { Type typeInfo = JobTypeMap[input]; var instance = Assembly.GetAssembly(typeInfo).CreateInstance(typeInfo.FullName); ConsoleHelper.WriteLineByColor($"Starting in {DateTime.Now},command by [{input}]", ConsoleColor.DarkGray); typeInfo.InvokeMember("Execute" , BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance , null , instance , null); ConsoleHelper.WriteLineByColor($"Ending in {DateTime.Now},command by [{input}]", ConsoleColor.DarkGray); } else { PrintErrorCommandMessage(); } }