{"record":{"id":"51ef3e8079adc53e","repo":"PrismLibrary/Prism","slug":"resources-cannotregistercompositecommandinitself","errorCode":null,"errorMessage":"Resources.CannotRegisterCompositeCommandInItself","messagePattern":"Resources\\.CannotRegisterCompositeCommandInItself","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Prism.Core/Commands/CompositeCommand.cs","lineNumber":54,"sourceCode":"        {\n            _monitorCommandActivity = monitorCommandActivity;\n        }\n\n        /// <summary>\n        /// Adds a command to the collection and signs up for the <see cref=\"ICommand.CanExecuteChanged\"/> event of it.\n        /// </summary>\n        ///  <remarks>\n        /// If this command is set to monitor command activity, and <paramref name=\"command\"/> \n        /// implements the <see cref=\"IActiveAware\"/> interface, this method will subscribe to its\n        /// <see cref=\"IActiveAware.IsActiveChanged\"/> event.\n        /// </remarks>\n        /// <param name=\"command\">The command to register.</param>\n        public virtual void RegisterCommand(ICommand command)\n        {\n            if (command == null) throw new ArgumentNullException(nameof(command));\n            if (command == this)\n            {\n                throw new ArgumentException(Resources.CannotRegisterCompositeCommandInItself);\n            }\n\n            lock (_registeredCommands)\n            {\n                if (_registeredCommands.Contains(command))\n                {\n                    throw new InvalidOperationException(Resources.CannotRegisterSameCommandTwice);\n                }\n                _registeredCommands.Add(command);\n            }\n\n            command.CanExecuteChanged += _onRegisteredCommandCanExecuteChangedHandler;\n            OnCanExecuteChanged();\n\n            if (_monitorCommandActivity)\n            {\n                if (command is IActiveAware activeAwareCommand)\n                {","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Prism.Core/Commands/CompositeCommand.cs#L36-L72","documentation":"CompositeCommand.RegisterCommand throws ArgumentException (Resources.CannotRegisterCompositeCommandInItself) when a composite command is registered into itself, which would cause infinite recursion during Execute/CanExecute voting.","triggerScenarios":"compositeCommand.RegisterCommand(compositeCommand) — e.g. accidentally assigning the composite into its own collection through a variable that references the same instance.","commonSituations":"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.","solutions":["Pass a distinct child ICommand instance, never the composite itself.","Verify the variable passed to RegisterCommand is not the same reference.","Refactor to keep child commands in a separate list to avoid accidental self-registration."],"exampleFix":"// before\nvar composite = new CompositeCommand();\ncomposite.RegisterCommand(composite); // self registration\n// after\nvar composite = new CompositeCommand();\ncomposite.RegisterCommand(new DelegateCommand(DoWork));","handlingStrategy":"validation","validationCode":"if (!ReferenceEquals(composite, childCommand))\n    composite.RegisterCommand(childCommand);","typeGuard":null,"tryCatchPattern":"try { composite.RegisterCommand(child); }\ncatch (ArgumentException ex) { logger.LogError(ex, \"attempted composite self-registration\"); }","preventionTips":["Keep composite parents and child commands in separate variables/lists.","Review wiring code for copy-paste reference mistakes."],"tags":["csharp","commands","recursive-registration"],"backgroundTag":"invalid-argument-value","analyzedSha":"358118cd640d9a22ff8cf21c8ad197fa038b7990","analyzedAt":"2026-09-15T15:00:31.079Z","contentChangedAt":"2026-09-15T15:00:31.079Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}