{"record":{"id":"e2a7ed001375d534","repo":"dotnet/reactive","slug":"could-not-find-instance-event-0-on-type-1","errorCode":null,"errorMessage":"Could not find instance event '{0}' on type '{1}'.","messagePattern":"Could not find instance event '(.+?)' on type '(.+?)'\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/FromEventPattern.cs","lineNumber":276,"sourceCode":"\n            return SynchronizeEvents(res, scheduler);\n        }\n\n        private static (MethodInfo addMethod, MethodInfo removeMethod, Type delegateType, bool isWinRT) GetEventMethods<TSender, TEventArgs>(Type targetType, object target, string eventName)\n        {\n            EventInfo e;\n\n            if (target == null)\n            {\n                e = targetType.GetEventEx(eventName, isStatic: true);\n                if (e == null)\n                    throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, \"Could not find static event '{0}' on type '{1}'.\", eventName, targetType.FullName));\n            }\n            else\n            {\n                e = targetType.GetEventEx(eventName, isStatic: false);\n                if (e == null)\n                    throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, \"Could not find instance event '{0}' on type '{1}'.\", eventName, targetType.FullName));\n            }\n\n            var addMethod = e.GetAddMethod();\n            var removeMethod = e.GetRemoveMethod();\n\n            if (addMethod == null)\n                throw new InvalidOperationException(\"Missing add method on event.\");\n            if (removeMethod == null)\n                throw new InvalidOperationException(\"Missing remove method on event.\");\n\n            var psa = addMethod.GetParameters();\n            if (psa.Length != 1)\n                throw new InvalidOperationException(\"The add method of an event should take one parameter.\");\n\n            var psr = removeMethod.GetParameters();\n            if (psr.Length != 1)\n                throw new InvalidOperationException(\"The remove method of an event should take one parameter.\");\n","sourceCodeStart":258,"sourceCodeEnd":294,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/FromEventPattern.cs#L258-L294","documentation":"GetEventMethods throws InvalidOperationException when no instance event with the given name exists on the target object's type. The lookup via GetEventEx(isStatic: false) returned null, so the subscription cannot proceed.","triggerScenarios":"Calling FromEventPattern(target, eventName) where eventName doesn't match any declared instance event: misspelling, case mismatch, event inherited in a way GetEventEx doesn't traverse, or the event removed in a newer library version.","commonSituations":"String-typed event names breaking silently after refactoring; targeting the wrong type (e.g. typeof(Base) when the event is declared on Derived); event only existing on WinRT/COM projection types.","solutions":["Use nameof to pass a compile-checked event name","Confirm the event is declared (or publicly inherited) on the exact type passed; pass typeof(Derived) or the target's GetType()","Use the debugger/reflection (type.GetEvents()) to list available events and pick the correct one"],"exampleFix":"// before\nvar obs = AsyncObservable.FromEventPattern<object, EventArgs>(control, \"Clickd\");\n// after\nvar obs = AsyncObservable.FromEventPattern<object, EventArgs>(control, nameof(Control.Click));","handlingStrategy":"validation","validationCode":"var ev = target.GetType().GetEvent(eventName, BindingFlags.Public | BindingFlags.Instance);\nif (ev is null) throw new ArgumentException($\"Instance event '{eventName}' not found on {target.GetType()}.\", nameof(eventName));","typeGuard":"static bool HasInstanceEvent(object target, string name) => target?.GetType().GetEvent(name, BindingFlags.Public | BindingFlags.Instance) is not null;","tryCatchPattern":"try { var obs = AsyncObservable.FromEventPattern<TSender, TEventArgs>(target, eventName); }\ncatch (InvalidOperationException ex) { log.LogError(ex, \"Instance event {Event} missing on {Type}\", eventName, target.GetType()); throw; }","preventionTips":["Use nameof(Control.Click) style expressions","Verify the event is declared on the runtime type, not just a base type","Avoid hardcoded event name strings in config where possible"],"tags":["csharp","reflection","event-not-found","invalid-operation"],"backgroundTag":"resource-not-found","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}