I've created a menu option class for a console application and want to bind the execution method to the option so when the user makes the selection I can do something like selectedOption.Execute(). The option's execute method would be bound to the appropriate method in a utility class. Is this possible? This is what I have so far.
MenuOption Class
public class MenuOption
{
public MenuOption()
{
}
public MenuOption(ConsoleKey key, string optionLabel, string optionText)
{
Key = key;
OptionLabel = optionLabel;
OptionText = optionText;
}
public MenuOption(ConsoleKey key, string optionLabel, string optionText, List<MenuOption> subMenu)
{
Key = key;
OptionLabel = optionLabel;
OptionText = optionText;
SubMenu = subMenu;
}
public ConsoleKey? Key { get; set; } = null;
public string OptionLabel { get; set; } = string.Empty;
public string OptionText { get; set; } = string.Empty;
public List<MenuOption> SubMenu { get; set; } = new List<MenuOption>();
public string Display()
{
StringBuilder menu = new StringBuilder();
menu.AppendLine("Press the number associated with the option of your choice or [Esc] to exit.");
foreach (MenuOption option in SubMenu)
{
if (option.Key == ConsoleKey.Escape)
{
menu.AppendLine($"{option.OptionLabel} {option.OptionText}");
}
else
{
menu.AppendLine($"{option.OptionLabel}. {option.OptionText}");
}
}
return menu.ToString();
}
}