public CommandExecutor(ICommandExpressionParser parser = null)
 {
     _root   = new CommandTreeNode();
     _parser = parser ?? CommandExpressionParser.Instance;
     _output = NullCommandOutlet.Instance;
     _error  = CommandErrorWriter.Instance;
 }
        public CommandTreeNode(ICommand command, CommandTreeNode parent = null) : base(command.Name, parent)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            _command  = command;
            _children = new CommandTreeNodeCollection(this);
        }
        public void Load(CommandTreeNode node)
        {
            if (node == null || _isLoaded)
            {
                return;
            }

            lock (_syncRoot)
            {
                if (_isLoaded)
                {
                    return;
                }

                _isLoaded = this.OnLoad(node);
            }
        }
        private CommandTreeNode FindDown(CommandTreeNode current, Predicate <CommandTreeNode> predicate)
        {
            if (current == null || predicate == null)
            {
                return(null);
            }

            if (predicate(current))
            {
                return(current);
            }

            foreach (var child in current._children)
            {
                if (this.FindDown(child, predicate) != null)
                {
                    return(child);
                }
            }

            return(null);
        }
Exemple #5
0
        public CommandContext(ICommandExecutor executor, CommandExpression expression, CommandTreeNode commandNode, object parameter, IDictionary <string, object> extendedProperties = null)
        {
            if (commandNode == null)
            {
                throw new ArgumentNullException("commandNode");
            }

            if (commandNode.Command == null)
            {
                throw new ArgumentException(string.Format("The Command property of '{0}' command-node is null.", commandNode.FullPath));
            }

            _executor    = executor;
            _commandNode = commandNode;
            _command     = commandNode.Command;
            _parameter   = parameter;
            _expression  = expression;

            if (extendedProperties != null && extendedProperties.Count > 0)
            {
                _extendedProperties = new Dictionary <string, object>(extendedProperties, StringComparer.OrdinalIgnoreCase);
            }
        }
 /// <summary>
 /// 执行加载命令的实际操作。
 /// </summary>
 /// <param name="node">待加载的命令树节点。</param>
 /// <returns>如果加载成功则返回真(true),否则返回假(false)。</returns>
 protected abstract bool OnLoad(CommandTreeNode node);
 public CommandTreeNode(string name, CommandTreeNode parent = null) : base(name, parent)
 {
     _children = new CommandTreeNodeCollection(this);
 }
 protected virtual CommandContext CreateCommandContext(CommandExpression expression, CommandTreeNode node, object parameter)
 {
     return(new CommandContext(this, expression, node, parameter));
 }
        protected virtual object ExecuteCommand(CommandExecutorContext context, CommandExpression expression, CommandTreeNode node, object parameter)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (node == null)
            {
                throw new ArgumentNullException(nameof(node));
            }

            if (node.Command == null)
            {
                return(null);
            }

            return(node.Command.Execute(this.CreateCommandContext(expression, node, parameter)));
        }