I have a few classes that implement the ITerminalCommand interface:
public class TerminalCommandHelp : MonoBehaviour, ITerminalCommand { //... }
public class TerminalCommandExit : MonoBehaviour, ITerminalCommand { //... }
In a separate class, I would like to be able to look up a string in a dictionary and create a new instance of the ITerminalCommand, but I don't know how I would go about doing this, so below I have written some sort of pseudo-code in the hopes that someone might understand what I am trying to do.
Dictionary<string, ITerminalCommand> validInputs = new Dictionary<string, ITerminalCommand>()
{
{
"help",
//Some reference to the TerminalCommandHelp class so I can instantiate it at my own will
},
{
"exit",
//Some reference to the TerminalCommandExit class so I can instantiate it at my own will
}
};
//Create a new object based on the key I am looking up, and supply some arguments to the constructor
ITerminalCommand genericTerminalCommand = new validInputs["help"](arguments);
//Run a method on the newly instantiated object
genericTerminalCommand.Execute();
How can I 'abstractly' reference a class type in a dictionary so I can instantiate it and also supply some arguments to it?