private ConsoleMenu GetConsoleMenu() { List <Type> processorTypes = new AssemblyScanner() .GetTypesWithAttribute <ProcessorAttribute>() .OrderBy(t => t.Name) .ToList(); var consoleMenuItems = new List <ConsoleMenuItem>(); List <string> categories = processorTypes .Select(t => { ProcessorAttribute processorAttribute = (ProcessorAttribute)t.GetCustomAttributes(typeof(ProcessorAttribute), true).Single(); return(processorAttribute.Category ?? GetDefaultCategory(t)); }) .OrderBy(s => s) .Distinct() .ToList(); for (int i = 0; i < categories.Count; i++) { string key = (i + 1).ToString(); string name = categories[i]; var subMenuItems = new List <ConsoleSubMenuItem>(); List <Type> processorTypesMatched = processorTypes .Where(t => { ProcessorAttribute processorAttr = (ProcessorAttribute)t.GetCustomAttributes(typeof(ProcessorAttribute), true).Single(); return((processorAttr.Category ?? GetDefaultCategory(t)) == name); }) .ToList(); for (int j = 0; j < processorTypesMatched.Count; j++) { Type t = processorTypesMatched[j]; ProcessorAttribute processorAttr = (ProcessorAttribute)t.GetCustomAttributes(typeof(ProcessorAttribute), true).Single(); string k = processorAttr.Key ?? (j + 1).ToString(); string n = processorAttr.Name ?? t.Name; k = ToSingleCharacter(k); var menuItem = new ConsoleSubMenuItem(k, n, t); subMenuItems.Add(menuItem); } key = ToSingleCharacter(key); var consoleMenuItem = new ConsoleMenuItem(key, name, subMenuItems); consoleMenuItems.Add(consoleMenuItem); } return(new ConsoleMenu(consoleMenuItems)); }
public async Task RunAsync(ConsoleMenuItem consoleMenuItem, CancellationToken cancellationToken) { do { DisplaySubMenu(consoleMenuItem); ConsoleKeyInfo consoleKeyInfo = Console.ReadKey(); string selection = consoleKeyInfo.KeyChar.ToString(); if (selection.ToLower() == "h") { break; } else { ConsoleSubMenuItem consoleSubMenuItem = consoleMenuItem.Processors.SingleOrDefault(s => string.Equals(s.Key, selection, StringComparison.OrdinalIgnoreCase)); if (consoleSubMenuItem == null) { Console.Clear(); Console.WriteLine("Invalid selection! Please try again (press any key to continue)"); Console.ReadKey(); Console.Clear(); } else { Console.Clear(); IServiceProvider scopedServiceProvider = _serviceProvider.CreateScope().ServiceProvider; object processor = scopedServiceProvider.GetService(consoleSubMenuItem.ProcessorType); if (!processor.GetType().GetInterfaces().Contains(typeof(IProcessor <,>)) && !processor.GetType().GetInterfaces().Contains(typeof(IProcessor <>)) && !processor.GetType().GetInterfaces().Contains(typeof(IProcessor))) { Console.WriteLine("Error! That selection is not a processor (press any key to continue)"); Console.ReadKey(); Console.Clear(); } else if (processor.GetType().GetInterfaces().Contains(typeof(IProcessor <,>)) || processor.GetType().GetInterfaces().Contains(typeof(IProcessor <>))) { InitializeProcessor(processor, scopedServiceProvider); Type inputType = processor.GetType().GenericTypeArguments[0]; object input = Activator.CreateInstance(inputType); await ExecuteAsync(async() => { await((dynamic)processor).ProcessAsync(input, cancellationToken); }); } else { InitializeProcessor(processor, scopedServiceProvider); await ExecuteAsync(async() => { await((IProcessor)processor).ProcessAsync(cancellationToken); }); } Console.WriteLine(); Console.WriteLine("***Complete***"); Console.WriteLine("Press any key to continue"); Console.ReadKey(); } } } while (true); }