public void Execute(string command) { string[] parts = command.Split(new char[] { ' ' }); IBookCommand cmd = this.commands.Where(c => c.CommandKey == parts[0]).FirstOrDefault(); if (cmd == null) { Console.WriteLine(string.Format("The command '{0}' couldn't be found.", parts[0])); } else { cmd.Execute(parts.Skip(1).ToArray()); } }
private void LoadCommands() { this.commands = new List <IBookCommand>(); Type interfaceType = typeof(IBookCommand); Type[] types = AppDomain.CurrentDomain.GetAssemblies() .SelectMany(s => s.GetTypes()) .Where(p => interfaceType.IsAssignableFrom(p) && p.IsClass) .ToArray(); foreach (Type type in types) { IBookCommand command = (IBookCommand)Activator.CreateInstance(type); command.Manager = this._manager; this.commands.Add(command); } }