Example #1
0
        public void Unregister(Command command)
        {
            if (command == null)
                throw new ArgumentNullException("command");

            Unregister(command.GetType());
        }
Example #2
0
        public void Register(Command command)
        {
            if (Application.IsRunning)
                throw new InvalidOperationException("The application is running.");

            if (command == null)
                throw new ArgumentNullException("command");

            try {
                command.Init();
            } catch(Exception e) {
                throw new ArgumentException("An error occurred while initializing the command: " + e.Message);
            }

            if (command is IInterruptable)
                Application.Interruption.Push(command as IInterruptable);

            if (command.Application != null &&
                command.Application != application)
                throw new ArgumentException("The command instance is already registered by another application.");

            command.SetApplicationContext(application);

            CommandInfo commandInfo = new CommandInfo(command.GetType(), command);
            commands.Add(commandInfo);
            string name = commandInfo.Command.Name;
            commandMap.Add(name, commandInfo.Command);
            if (commandInfo.Command.HasAliases) {
                string[] cmdAliases = commandInfo.Command.Aliases;
                for (int i = 0; i < cmdAliases.Length; ++i) {
                    if (commandMap.ContainsKey(cmdAliases[i]))
                        throw new ArgumentException("attempt to register command '" + cmdAliases[i] + "', that is already used");

                    commandMap.Add(cmdAliases[i], commandInfo.Command);
                }
            }
        }