{"record":{"id":"770cfaea9963ae30","repo":"dotnet/wpf","slug":"sr-format-sr-invalid-iinputelement-e-gettype","errorCode":null,"errorMessage":"SR.Format(SR.Invalid_IInputElement, e.GetType())","messagePattern":"SR\\.Format\\(SR\\.Invalid_IInputElement, e\\.GetType\\(\\)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/PresentationSource.cs","lineNumber":185,"sourceCode":"\n        /// <summary>\n        ///     Removes a handler for the SourceChanged event to the element.\n        /// </summary>\n        /// <param name=\"e\">The element to remove the handler from.</param>\n        /// <param name=\"handler\">The hander to remove.</param>\n        /// <remarks>\n        ///     Even though this is a routed event handler, there are special\n        ///     restrictions placed on this event.\n        ///     1) You cannot use the UIElement or ContentElement RemoveHandler() method.\n        /// </remarks>\n        public static void RemoveSourceChangedHandler(IInputElement e, SourceChangedEventHandler handler)\n        {\n            ArgumentNullException.ThrowIfNull(e);\n\n            // Either UIElement, ContentElement or UIElement3D.\n            if (!InputElement.IsValid(e))\n            {\n                throw new ArgumentException(SR.Format(SR.Invalid_IInputElement, e.GetType()), nameof(e));\n            }\n            DependencyObject o = (DependencyObject)e;\n\n            //             o.VerifyAccess();\n\n            // I would rather throw an exception here, but the CLR doesn't\n            // so we won't either.\n            if (handler != null)\n            {\n                FrugalObjectList<RoutedEventHandlerInfo> info = null;\n                EventHandlersStore store;\n\n\n                // Either UIElement, ContentElement or UIElement3D.\n                if (o is UIElement uie)\n\n                {\n                    uie.RemoveHandler(SourceChangedEvent, handler);","sourceCodeStart":167,"sourceCodeEnd":203,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/PresentationSource.cs#L167-L203","documentation":"PresentationSource.RemoveSourceChangedHandler applies the same validity rule as the Add counterpart: the element must be a UIElement, ContentElement, or UIElement3D. Otherwise an ArgumentException with the element's type is thrown, even when attempting to unsubscribe.","triggerScenarios":"Calling RemoveSourceChangedHandler with an unsupported IInputElement implementer or an object of the wrong kind (e.g. a raw DependencyObject that is not an input element).","commonSituations":"Symmetric cleanup code that removes handlers from the same mocked/unrecognized element it subscribed with; refactoring that changed element base classes from UIElement to a custom class.","solutions":["Unsubscribe using exactly the same UIElement/ContentElement/UIElement3D instance used at subscription time.","Guard removal with the same type check used before subscribing.","Revert custom element types to standard WPF element classes.","Wrap cleanup in a type check to skip unsupported elements rather than assuming add succeeded."],"exampleFix":"// before\nPresentationSource.RemoveSourceChangedHandler(unknownElement, handler);\n\n// after\nif (unknownElement is UIElement || unknownElement is ContentElement || unknownElement is UIElement3D)\n{\n    PresentationSource.RemoveSourceChangedHandler(unknownElement, handler);\n}","handlingStrategy":"type-guard","validationCode":"if (e is UIElement || e is ContentElement || e is UIElement3D)\n    PresentationSource.RemoveSourceChangedHandler(e, handler);","typeGuard":"static bool CanUnsubscribe(IInputElement e) =>\n    e is UIElement || e is ContentElement || e is UIElement3D;","tryCatchPattern":"try { PresentationSource.RemoveSourceChangedHandler(e, handler); }\ncatch (ArgumentException ex) when (ex.ParamName == \"e\") { /* skip unsupported element */ }","preventionTips":["Store the exact typed element used at subscription for later unsubscription.","Apply identical validation in add and remove paths.","Avoid changing element base classes without updating handler bookkeeping."],"tags":["wpf","argument-validation","event-unsubscription"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}