dotnet/reactive · error · InvalidOperationException

Could not find instance event

Error message

Could not find instance event '{0}' on type '{1}'.

What it means

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.

Solutions

  1. Use nameof to pass a compile-checked event name
  2. Confirm the event is declared (or publicly inherited) on the exact type passed; pass typeof(Derived) or the target's GetType()
  3. Use the debugger/reflection (type.GetEvents()) to list available events and pick the correct one

Example fix

// before
var obs = AsyncObservable.FromEventPattern<object, EventArgs>(control, "Clickd");
// after
var obs = AsyncObservable.FromEventPattern<object, EventArgs>(control, nameof(Control.Click));
Defensive patterns

Strategy: validation

Validate before calling

var ev = target.GetType().GetEvent(eventName, BindingFlags.Public | BindingFlags.Instance);
if (ev is null) throw new ArgumentException($"Instance event '{eventName}' not found on {target.GetType()}.", nameof(eventName));

Type guard

static bool HasInstanceEvent(object target, string name) => target?.GetType().GetEvent(name, BindingFlags.Public | BindingFlags.Instance) is not null;

Try / catch

try { var obs = AsyncObservable.FromEventPattern<TSender, TEventArgs>(target, eventName); }
catch (InvalidOperationException ex) { log.LogError(ex, "Instance event {Event} missing on {Type}", eventName, target.GetType()); throw; }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.

Related errors


AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15). Data as JSON: /api/errors/e2a7ed001375d534. Report an issue: GitHub.

Appendix: source

Thrown at AsyncRx.NET/System.Reactive.Async/Linq/Operators/FromEventPattern.cs:276

            return SynchronizeEvents(res, scheduler);
        }

        private static (MethodInfo addMethod, MethodInfo removeMethod, Type delegateType, bool isWinRT) GetEventMethods<TSender, TEventArgs>(Type targetType, object target, string eventName)
        {
            EventInfo e;

            if (target == null)
            {
                e = targetType.GetEventEx(eventName, isStatic: true);
                if (e == null)
                    throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "Could not find static event '{0}' on type '{1}'.", eventName, targetType.FullName));
            }
            else
            {
                e = targetType.GetEventEx(eventName, isStatic: false);
                if (e == null)
                    throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, "Could not find instance event '{0}' on type '{1}'.", eventName, targetType.FullName));
            }

            var addMethod = e.GetAddMethod();
            var removeMethod = e.GetRemoveMethod();

            if (addMethod == null)
                throw new InvalidOperationException("Missing add method on event.");
            if (removeMethod == null)
                throw new InvalidOperationException("Missing remove method on event.");

            var psa = addMethod.GetParameters();
            if (psa.Length != 1)
                throw new InvalidOperationException("The add method of an event should take one parameter.");

            var psr = removeMethod.GetParameters();
            if (psr.Length != 1)
                throw new InvalidOperationException("The remove method of an event should take one parameter.");

View on GitHub (pinned to 94b5d5ab91)