{"record":{"id":"d38205c9878eedc2","repo":"dotnet/reactive","slug":"the-event-arguments-parameter-of-the-event-is-not-assignable","errorCode":null,"errorMessage":"The event arguments parameter of the event is not assignable to '{0}'.","messagePattern":"The event arguments parameter of the event is not assignable to '(.+?)'\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/FromEventPattern.cs","lineNumber":319,"sourceCode":"                var pet = psr[0];\n                if (pet.ParameterType != addMethod.ReturnType)\n                    throw new InvalidOperationException(\"An event should either have add and remove methods that return void or an add method that returns a type compatible with the parameter type of the remove method.\");\n            }\n\n            var delegateType = psa[0].ParameterType;\n\n            var invokeMethod = delegateType.GetMethod(\"Invoke\");\n\n            var parameters = invokeMethod.GetParameters();\n\n            if (parameters.Length != 2)\n                throw new InvalidOperationException(\"The delegate type for an event conforming to the traditional event pattern should take two parameters.\");\n\n            if (!typeof(TSender).IsAssignableFrom(parameters[0].ParameterType))\n                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, \"The sender parameter of the event is not assignable to '{0}'.\", typeof(TSender).FullName));\n\n            if (!typeof(TEventArgs).IsAssignableFrom(parameters[1].ParameterType))\n                throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, \"The event arguments parameter of the event is not assignable to '{0}'.\", typeof(TEventArgs).FullName));\n\n            if (invokeMethod.ReturnType != typeof(void))\n                throw new InvalidOperationException(\"The return type of an event delegate should be void.\");\n\n            return (addMethod, removeMethod, delegateType, isWinRT);\n        }\n\n        public static EventInfo GetEventEx(this Type type, string name, bool isStatic)\n        {\n            return type.GetEvent(name, isStatic ? BindingFlags.Public | BindingFlags.Static : BindingFlags.Public | BindingFlags.Instance);\n        }\n    }\n}\n","sourceCodeStart":301,"sourceCodeEnd":333,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/FromEventPattern.cs#L301-L333","documentation":"GetEventMethods checks that the delegate's second parameter (the event arguments) is assignable to TEventArgs. This InvalidOperationException is thrown when the delegate's event-args parameter type does not derive from / match the TEventArgs generic argument, so the adapter cannot convert the event payload for the observable.","triggerScenarios":"Calling FromEventPattern<TSender, TEventArgs> where TEventArgs is a base class (e.g. EventArgs) but the delegate's second parameter is a derived type the caller failed to specify, or vice versa — a completely unrelated TEventArgs type.","commonSituations":"Using the non-generic EventArgs as TEventArgs with a delegate carrying specific args; typos in generic arguments; events whose delegate uses a custom args class not related to the one supplied.","solutions":["Set TEventArgs to the exact type (or a base type) of the delegate's second Invoke parameter, e.g. FromEventPattern<object, KeyEventArgs>","Check typeof(D).GetMethod(\"Invoke\").GetParameters()[1].ParameterType and ensure TEventArgs.IsAssignableFrom(that type)","Prefer the EventHandler<TEventArgs>-based overloads which infer TEventArgs correctly"],"exampleFix":"// before\nAsyncObservable.FromEventPattern<object, EventArgs>(h => keySrc.PreviewKeyDown += h, h => keySrc.PreviewKeyDown -= h); // args are KeyEventArgs\n// after\nAsyncObservable.FromEventPattern<object, KeyEventArgs>(h => keySrc.PreviewKeyDown += h, h => keySrc.PreviewKeyDown -= h);","handlingStrategy":"validation","validationCode":"var p = typeof(MyDelegate).GetMethod(\"Invoke\").GetParameters();\nif (!typeof(TEventArgs).IsAssignableFrom(p[1].ParameterType))\n    throw new InvalidOperationException($\"TEventArgs must accept {p[1].ParameterType}\");","typeGuard":"static bool EventArgsCompatible<TEventArgs>(Type d)\n    => typeof(TEventArgs).IsAssignableFrom(d.GetMethod(\"Invoke\")?.GetParameters()[1].ParameterType);","tryCatchPattern":"try\n{\n    var obs = AsyncObservable.FromEventPattern<TSender, TEventArgs>(add, remove);\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"arguments parameter\"))\n{\n    // correct TEventArgs to the delegate's actual args type and retry\n}","preventionTips":["Set TEventArgs to the exact concrete args type of the event","Prefer EventHandler<TEventArgs>-based overloads that infer types","Compile-time check: lambda h => (s, e) => h(s, e) forces correct types"],"tags":["reflection","generics","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}