{"record":{"id":"0c75e89a07ab5de2","repo":"dotnet/wpf","slug":"sr-format-sr-invalid-iinputelement-o-gettype-0c75e8","errorCode":null,"errorMessage":"SR.Format(SR.Invalid_IInputElement, o.GetType())","messagePattern":"SR\\.Format\\(SR\\.Invalid_IInputElement, o\\.GetType\\(\\)\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/PresentationSource.cs","lineNumber":163,"sourceCode":"                    info = uie3D.EventHandlersStore[SourceChangedEvent];\n                    if (1 == info.Count)\n                    {\n                        uie3D.VisualAncestorChanged += new Visual.AncestorChangedEventHandler(uie3D.OnVisualAncestorChanged);\n                        AddElementToWatchList(uie3D);\n                    }\n                }\n                else if (o is ContentElement ce)\n                {\n                    ce.AddHandler(SourceChangedEvent, handler);\n                    info = ce.EventHandlersStore[SourceChangedEvent];\n                    if (1 == info.Count)\n                    {\n                        AddElementToWatchList(ce);\n                    }\n                }\n                else\n                {\n                    throw new InvalidOperationException(SR.Format(SR.Invalid_IInputElement, o.GetType())); \n                }\n            }\n        }\n\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","sourceCodeStart":145,"sourceCodeEnd":181,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/PresentationSource.cs#L145-L181","documentation":"Inside AddSourceChangedHandler, after the top-level type check, the element is dispatched by concrete type (UIElement, ContentElement, UIElement3D). If the object somehow passes InputElement.IsValid yet matches none of these three concrete classes, an InvalidOperationException is thrown - an internal invariant fallback.","triggerScenarios":"Passing an element type for which the type-switch has no case (a type recognized as valid IInputElement but not one of the three known classes) - typically only possible with exotic third-party implementations of the internal input contracts.","commonSituations":"Using framework versions where new IInputElement implementers exist but the switch in PresentationSource was not updated; custom frameworks reimplementing core element types; assembly-mix mismatches between WPF assemblies (e.g. different PresentationCore versions loaded).","solutions":["Ensure only UIElement, ContentElement, or UIElement3D instances are subscribed.","Verify all WPF assemblies resolve to the same version to avoid type-identity mismatches.","Replace custom element base classes with the standard WPF element classes.","If unavoidable, subscribe at a known UIElement ancestor instead of the exotic element."],"exampleFix":"// before\nPresentationSource.AddSourceChangedHandler(customElementBase, handler);\n\n// after\nPresentationSource.AddSourceChangedHandler((UIElement)customElementBase, handler);","handlingStrategy":"type-guard","validationCode":"if (!(o is UIElement || o is ContentElement || o is UIElement3D)) return;","typeGuard":"static bool IsKnownInputElement(DependencyObject o) =>\n    o is UIElement || o is ContentElement || o is UIElement3D;","tryCatchPattern":null,"preventionTips":["Ensure all WPF assemblies are the same version (check binding redirects).","Avoid third-party re-implementations of WPF core element types.","Subscribe only on standard WPF element classes."],"tags":["wpf","internal-invariant","type-mismatch"],"backgroundTag":"internal-invariant-violation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}