PrismLibrary/Prism · error · ArgumentException

Resources.CannotRegisterCompositeCommandInItself

Error message

Resources.CannotRegisterCompositeCommandInItself

What it means

CompositeCommand.RegisterCommand throws ArgumentException (Resources.CannotRegisterCompositeCommandInItself) when a composite command is registered into itself, which would cause infinite recursion during Execute/CanExecute voting.

Solutions

  1. Pass a distinct child ICommand instance, never the composite itself.
  2. Verify the variable passed to RegisterCommand is not the same reference.
  3. Refactor to keep child commands in a separate list to avoid accidental self-registration.

Example fix

// before
var composite = new CompositeCommand();
composite.RegisterCommand(composite); // self registration
// after
var composite = new CompositeCommand();
composite.RegisterCommand(new DelegateCommand(DoWork));
Defensive patterns

Strategy: validation

Validate before calling

if (!ReferenceEquals(composite, childCommand))
    composite.RegisterCommand(childCommand);

Try / catch

try { composite.RegisterCommand(child); }
catch (ArgumentException ex) { logger.LogError(ex, "attempted composite self-registration"); }

Prevention

When it happens

Trigger: compositeCommand.RegisterCommand(compositeCommand) — e.g. accidentally assigning the composite into its own collection through a variable that references the same instance.

Common situations: Copy-paste wiring errors where the same variable is used for parent and child; factory methods returning the composite itself instead of a child command.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of PrismLibrary/Prism@358118cd64 (2026-09-15). Data as JSON: /api/errors/51ef3e8079adc53e. Report an issue: GitHub.

Appendix: source

Thrown at src/Prism.Core/Commands/CompositeCommand.cs:54

        {
            _monitorCommandActivity = monitorCommandActivity;
        }

        /// <summary>
        /// Adds a command to the collection and signs up for the <see cref="ICommand.CanExecuteChanged"/> event of it.
        /// </summary>
        ///  <remarks>
        /// If this command is set to monitor command activity, and <paramref name="command"/> 
        /// implements the <see cref="IActiveAware"/> interface, this method will subscribe to its
        /// <see cref="IActiveAware.IsActiveChanged"/> event.
        /// </remarks>
        /// <param name="command">The command to register.</param>
        public virtual void RegisterCommand(ICommand command)
        {
            if (command == null) throw new ArgumentNullException(nameof(command));
            if (command == this)
            {
                throw new ArgumentException(Resources.CannotRegisterCompositeCommandInItself);
            }

            lock (_registeredCommands)
            {
                if (_registeredCommands.Contains(command))
                {
                    throw new InvalidOperationException(Resources.CannotRegisterSameCommandTwice);
                }
                _registeredCommands.Add(command);
            }

            command.CanExecuteChanged += _onRegisteredCommandCanExecuteChangedHandler;
            OnCanExecuteChanged();

            if (_monitorCommandActivity)
            {
                if (command is IActiveAware activeAwareCommand)
                {

View on GitHub (pinned to 358118cd64)