{"record":{"id":"c1a8ad411a575136","repo":"dotnet/reactive","slug":"could-not-find-static-event-0-on-type-1","errorCode":null,"errorMessage":"Could not find static event '{0}' on type '{1}'.","messagePattern":"Could not find static event '(.+?)' on type '(.+?)'\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/FromEventPattern.cs","lineNumber":270,"sourceCode":"                        return default;\n                    });\n\n                    return new ValueTask<IAsyncDisposable>(dispose);\n                });\n            }\n\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)","sourceCodeStart":252,"sourceCodeEnd":288,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/FromEventPattern.cs#L252-L288","documentation":"GetEventMethods, the reflection core behind FromEventPattern, throws InvalidOperationException when no static event with the given name exists on the target type. Since no target object was supplied, the library looks up the event with BindingFlags for static members via GetEventEx and finds nothing.","triggerScenarios":"Calling the static FromEventPattern(Type, string) overload with an event name that is actually an instance event, a misspelled name, or an event that lives on a base type/interface not considered by GetEventEx.","commonSituations":"Typo in event name string (no compile-time check because events are passed as strings), renamed event after a library upgrade, assuming the event is static when it is an instance event.","solutions":["Verify the event name spelling against the actual type declaration (events are passed as strings and not compile-checked)","If the event is an instance event, use the FromEventPattern overload that takes a target object instead","Use nameof(Source.EventName) to get a compile-checked name"],"exampleFix":"// before\nvar obs = AsyncObservable.FromEventPattern<object, EventArgs>(typeof(Source), \"Progres\"); // typo, and event is instance\n// after\nvar src = new Source();\nvar obs = AsyncObservable.FromEventPattern<object, EventArgs>(src, nameof(Source.Progress));","handlingStrategy":"validation","validationCode":"var ev = typeof(EventSource).GetEvent(eventName, BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);\nif (ev is null) throw new ArgumentException($\"Static event '{eventName}' not found on {typeof(EventSource)}.\", nameof(eventName));","typeGuard":"static bool HasStaticEvent(Type t, string name) => t.GetEvent(name, BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy) is not null;","tryCatchPattern":"try { var obs = AsyncObservable.FromEventPattern<TSender, TEventArgs>(type, eventName); }\ncatch (InvalidOperationException ex) { log.LogError(ex, \"Static event {Event} missing on {Type}\", eventName, type); throw; }","preventionTips":["Use nameof for compile-checked event names","Check BindingFlags.FlattenHierarchy for inherited static events","List available events with type.GetEvents() when unsure"],"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"}