{"record":{"id":"cb63432cc69e2cce","repo":"dotnet/reactive","slug":"the-add-method-of-an-event-should-take-one-parameter","errorCode":null,"errorMessage":"The add method of an event should take one parameter.","messagePattern":"The add method of an event should take one parameter\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/FromEventPattern.cs","lineNumber":289,"sourceCode":"            }\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\n            var isWinRT = false;\n\n            if (addMethod.ReturnType != typeof(void))\n            {\n                isWinRT = true;\n\n                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","sourceCodeStart":271,"sourceCodeEnd":307,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/FromEventPattern.cs#L271-L307","documentation":"The event's add accessor must take exactly one parameter (the delegate). GetEventMethods throws InvalidOperationException when addMethod.GetParameters() has a length other than 1, since the metadata violates the CLR event pattern and a handler delegate cannot be constructed.","triggerScenarios":"Reflecting over something that is not a well-formed event (e.g. a method pair mistakenly exposed as an 'event' via custom metadata, or hand-authored IL emitting an add method with extra parameters).","commonSituations":"Custom reflection-based event emitters, code-weaving/AOP tools rewriting accessor signatures, or passing the wrong EventInfo through a custom wrapper.","solutions":["Ensure the event follows the standard CLR event pattern: add(object delegate) with a single delegate parameter","Re-declare the event as 'public event SomeDelegate Name;' and let the compiler generate correct accessors","Inspect addMethod.GetParameters() yourself to diagnose the actual signature"],"exampleFix":"// before\n// hand-written accessor with extra param\npublic void add_Tick(EventHandler h, object token) { ... }\n// after\npublic event EventHandler Tick; // compiler-generated add/remove, one parameter each","handlingStrategy":"validation","validationCode":"var ev = type.GetEvent(eventName);\nvar add = ev?.GetAddMethod();\nif (add is null || add.GetParameters().Length != 1) throw new NotSupportedException($\"Event '{eventName}' add accessor must take exactly one delegate parameter.\");","typeGuard":"static bool HasValidAddSignature(Type t, string name) => t.GetEvent(name)?.GetAddMethod()?.GetParameters().Length == 1;","tryCatchPattern":null,"preventionTips":["Let the compiler generate event accessors (field-like events)","Avoid IL weavers that rewrite accessor signatures","Sanity-check interop/projection event metadata before subscribing"],"tags":["csharp","reflection","event-signature","invalid-operation"],"backgroundTag":"unsupported-operation","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"}