dotnet/reactive · error · InvalidOperationException

Strings_Linq.COULD_NOT_FIND_INSTANCE_EVENT

Error message

Strings_Linq.COULD_NOT_FIND_INSTANCE_EVENT

What it means

The instance-mode counterpart of error 2575: when a target instance is supplied, GetEventMethods searches public instance events and throws InvalidOperationException 'Could not find instance event <name> on <type>' if not found. Used by Observable.FromEventPattern with an instance target.

Solutions

  1. Correct the event name string to match the public instance event exactly (case-sensitive).
  2. If the event is static, pass null as the target to use the static lookup path.
  3. Switch to the generic FromEventPattern<TEventHandler, TArgs>(handler => obj.Event += handler, ...) overload to get compile-time checking instead of string reflection.

Example fix

// before
Observable.FromEventPattern(btn, "Clik", typeof(Button)); // typo
// after
Observable.FromEventPattern(btn, "Click", typeof(Button));
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

static bool HasInstanceEvent(object o, string name) => o.GetType().GetEvent(name, BindingFlags.Public | BindingFlags.Instance) != null;

Try / catch

try { Observable.FromEventPattern(target, eventName, targetType); } catch (InvalidOperationException ex) when (ex.Message.Contains("instance event")) { /* fix name or use static path */ }

Prevention

When it happens

Trigger: Observable.FromEventPattern(target, eventName, targetType) where eventName does not match a public instance event on the target's type.

Common situations: Typos or casing mismatches in event names; the event is static but a target was passed; event made private/internal; refactoring renamed the event while the string literal remained.

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/950cd203911e7051. Report an issue: GitHub.

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Internal/ReflectionUtils.cs:38

            return method.CreateDelegate(delegateType, o);
        }

#if HAS_TRIMMABILITY_ATTRIBUTES
    [RequiresUnreferencedCode(Constants_Core.EventReflectionTrimIncompatibilityMessage)]
#endif
        public static void GetEventMethods<TSender, TEventArgs>(Type targetType, object? target, string eventName, out MethodInfo addMethod, out MethodInfo removeMethod, out Type delegateType, out bool isWinRT)
        {
            EventInfo? e;

            if (target == null)
            {
                e = targetType.GetEvent(eventName, BindingFlags.Public | BindingFlags.Static)
                    ?? throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Strings_Linq.COULD_NOT_FIND_STATIC_EVENT, eventName, targetType.FullName));
            }
            else
            {
                e = targetType.GetEvent(eventName, BindingFlags.Public | BindingFlags.Instance)
                    ?? throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Strings_Linq.COULD_NOT_FIND_INSTANCE_EVENT, eventName, targetType.FullName));
            }

            addMethod = e.GetAddMethod() ?? throw new InvalidOperationException(Strings_Linq.EVENT_MISSING_ADD_METHOD);
            removeMethod = e.GetRemoveMethod() ?? throw new InvalidOperationException(Strings_Linq.EVENT_MISSING_REMOVE_METHOD);

            var psa = addMethod.GetParameters();
            if (psa.Length != 1)
            {
                throw new InvalidOperationException(Strings_Linq.EVENT_ADD_METHOD_SHOULD_TAKE_ONE_PARAMETER);
            }

            var psr = removeMethod.GetParameters();
            if (psr.Length != 1)
            {
                throw new InvalidOperationException(Strings_Linq.EVENT_REMOVE_METHOD_SHOULD_TAKE_ONE_PARAMETER);
            }

            isWinRT = false;

View on GitHub (pinned to 94b5d5ab91)