{"record":{"id":"983565b8cf5612de","repo":"dotnet/wpf","slug":"event-not-found-event-eventname-not-found-on-type","errorCode":null,"errorMessage":"Event not found: Event 'eventName' not found on type 'TEventSource'.","messagePattern":"Event not found: Event 'eventName' not found on type 'TEventSource'\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/WeakEventManagerT.cs","lineNumber":28,"sourceCode":"\nnamespace System.Windows\n{\n    public class WeakEventManager<TEventSource, TEventArgs> : WeakEventManager\n        where TEventArgs : EventArgs\n    {\n        #region Constructors\n\n        //\n        //  Constructors\n        //\n\n        private WeakEventManager(string eventName)\n        {\n            _eventName = eventName;\n            _eventInfo = typeof(TEventSource).GetEvent(_eventName);\n\n            if (_eventInfo == null)\n                throw new ArgumentException(SR.Format(SR.EventNotFound, typeof(TEventSource).FullName, eventName));\n\n            _handler = Delegate.CreateDelegate(_eventInfo.EventHandlerType, this, DeliverEventMethodInfo);\n        }\n\n        #endregion Constructors\n\n        #region Public Methods\n\n        //\n        //  Public Methods\n        //\n\n        /// <summary>\n        /// Add a handler for the given source's event.\n        /// </summary>\n        public static void AddHandler(TEventSource source, string eventName, EventHandler<TEventArgs> handler)\n        {\n            ArgumentNullException.ThrowIfNull(handler);","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/WeakEventManagerT.cs#L10-L46","documentation":"The private WeakEventManager<TEventSource,TEventArgs> constructor resolves the event by name via Type.GetEvent on TEventSource. If no public event with that name exists, _eventInfo is null and it throws ArgumentException with SR.EventNotFound ('Event not found: Event {type} not found on type {source}.'). This is a fail-fast guard so the reflection-based manager is never built against a nonexistent event.","triggerScenarios":"Calling WeakEventManager<TEventSource,TEventArgs>.AddHandler(source, \"Click\", handler) (or RemoveHandler / the constructor) where the string eventName does not match any public event on TEventSource — typo, case mismatch, event renamed, or event defined on a derived/interface type not exposed by TEventSource.","commonSituations":"Renaming an event during refactoring while string-based handler registrations still use the old name; using the CLR event name when the class exposes it under a different registered name; targeting events declared private/internal (GetEvent only finds public events).","solutions":["Verify the exact spelling and case of the event name against typeof(TEventSource) — use nameof(SourceType.EventName) instead of a literal string.","Confirm the event is public and declared on (or inherited by) TEventSource; GetEvent does not return non-public events.","If the event was renamed, update all AddHandler/RemoveHandler call sites to the new name.","Check with typeof(TEventSource).GetEvent(\"YourEvent\") in the debugger/immediate window to confirm the reflection lookup succeeds."],"exampleFix":"// before\nWeakEventManager<MyControl, RoutedEventArgs>.AddHandler(control, \"Clic\", OnClick);\n// after\nWeakEventManager<MyControl, RoutedEventArgs>.AddHandler(control, nameof(MyControl.Click), OnClick);","handlingStrategy":"validation","validationCode":"var eventInfo = typeof(TEventSource).GetEvent(eventName);\nif (eventInfo == null)\n    throw new InvalidOperationException($\"Event '{eventName}' does not exist on {typeof(TEventSource).FullName}. Available: {string.Join(\", \", typeof(TEventSource).GetEvents().Select(e => e.Name))}\");\n\nWeakEventManager<TEventSource, TEventArgs>.AddHandler(source, eventName, handler);","typeGuard":"static bool EventExists<TSrc>(string eventName) => typeof(TSrc).GetEvent(eventName) != null;","tryCatchPattern":"try\n{\n    WeakEventManager<TEventSource, TEventArgs>.AddHandler(source, eventName, handler);\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(\"Event not found\"))\n{\n    logger.LogError(ex, \"Event '{Event}' not found on {Type}\", eventName, typeof(TEventSource).Name);\n    throw;\n}","preventionTips":["Always use nameof(EventSource.EventName) instead of string literals.","Confirm the event is public and accessible on the exact source type.","When refactoring renames an event, search all WeakEventManager usages of the old string."],"tags":["wpf","reflection","events","weak-events"],"backgroundTag":"resource-not-found","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"}