{"record":{"id":"a51f4b3768f0bea6","repo":"dotnet/wpf","slug":"sr-handlertypeillegal-eventmanager","errorCode":null,"errorMessage":"SR.HandlerTypeIllegal","messagePattern":"SR\\.HandlerTypeIllegal","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/EventManager.cs","lineNumber":164,"sourceCode":"            Delegate handler,\n            bool handledEventsToo)\n        {\n            ArgumentNullException.ThrowIfNull(classType);\n\n            ArgumentNullException.ThrowIfNull(routedEvent);\n\n            ArgumentNullException.ThrowIfNull(handler);\n\n            if (!typeof(UIElement).IsAssignableFrom(classType) &&\n                !typeof(ContentElement).IsAssignableFrom(classType) &&\n                !typeof(UIElement3D).IsAssignableFrom(classType))\n            {\n                throw new ArgumentException(SR.ClassTypeIllegal);\n            }\n            \n            if (!routedEvent.IsLegalHandler(handler))\n            {\n                throw new ArgumentException(SR.HandlerTypeIllegal);\n            }\n            \n            GlobalEventManager.RegisterClassHandler(classType, routedEvent, handler, handledEventsToo);\n        }\n\n        /// <summary>\n        ///     Returns <see cref=\"RoutedEvent\"/>s \n        ///     that have been registered so far\n        /// </summary>\n        /// <remarks>\n        ///     Also see \n        ///     <see cref=\"EventManager.RegisterRoutedEvent\"/>\n        ///     <para/>\n        ///     <para/>\n        ///\n        ///     NOTE: There may be more \n        ///     <see cref=\"RoutedEvent\"/>s registered later\n        /// </remarks>","sourceCodeStart":146,"sourceCodeEnd":182,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/EventManager.cs#L146-L182","documentation":"In EventManager.RegisterClassHandler, after verifying the class type, the handler delegate is validated with routedEvent.IsLegalHandler; an incompatible delegate type throws ArgumentException with SR.HandlerTypeIllegal. The handler's signature must match the routed event's declared HandlerType.","triggerScenarios":"Calling RegisterClassHandler with a Delegate whose signature doesn't match routedEvent.HandlerType — e.g. a RoutedEventHandler for a keyboard event declared with KeyEventHandler, or a lambda cast to the wrong delegate type.","commonSituations":"Generic helper methods taking Delegate and passing through to RegisterClassHandler; copy-pasted registration lines across events with different handler types; refactors that change an event's handler type.","solutions":["Create the delegate as routedEvent.HandlerType (e.g. new KeyEventHandler(OnKey))","Check routedEvent.HandlerType before constructing the delegate","Use the correct event-specific handler delegate for the routed event"],"exampleFix":"// before\nEventManager.RegisterClassHandler(typeof(UIElement), Keyboard.KeyDownEvent,\n    new RoutedEventHandler(OnKeyDown), false);\n// after\nEventManager.RegisterClassHandler(typeof(UIElement), Keyboard.KeyDownEvent,\n    new KeyEventHandler(OnKeyDown), false);","handlingStrategy":"validation","validationCode":"if (routedEvent.IsLegalHandler(handler)) { EventManager.RegisterClassHandler(classType, routedEvent, handler, handledEventsToo); }","typeGuard":"bool IsLegalClassHandler(RoutedEvent e, Delegate h) => e != null && h != null && e.IsLegalHandler(h);","tryCatchPattern":"try { EventManager.RegisterClassHandler(typeof(UIElement), evt, handler, false); } catch (ArgumentException) { /* handler delegate type mismatch */ }","preventionTips":["Construct delegates with the event's HandlerType","Use event-specific handler types (KeyEventHandler, MouseButtonEventHandler, ...)","Type generic registration helpers against the correct delegate type"],"tags":["wpf","routed-events","class-handler","delegate"],"backgroundTag":"type-mismatch","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"}