{"record":{"id":"ab393aa6d9f1268e","repo":"devlooped/moq","slug":"resources-invalidcallbackparametermismatch-formatted-with","errorCode":null,"errorMessage":"Resources.InvalidCallbackParameterMismatch (formatted with expected and actual parameter type lists)","messagePattern":"Resources\\.InvalidCallbackParameterMismatch \\(formatted with expected and actual parameter type lists\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Moq/MethodCall.cs","lineNumber":166,"sourceCode":"                                                                     : ref this.afterReturnCallback;\n\n            if (callback is Action callbackWithoutArguments)\n            {\n                behavior = new Callback(_ => callbackWithoutArguments());\n            }\n            else if (callback.GetType() == typeof(Action<IInvocation>))\n            {\n                // NOTE: Do NOT rewrite the above condition as `callback is Action<IInvocation>`,\n                // because this will also yield true if `callback` is a `Action<object>` and thus\n                // break existing uses of `(object arg) => ...` callbacks!\n                behavior = new Callback((Action<IInvocation>)callback);\n            }\n            else\n            {\n                var expectedParamTypes = this.Method.GetParameterTypes();\n                if (!callback.CompareParameterTypesTo(expectedParamTypes))\n                {\n                    throw new ArgumentException(\n                        string.Format(\n                            CultureInfo.CurrentCulture,\n                            Resources.InvalidCallbackParameterMismatch,\n                            this.Method.GetParameterTypeList(),\n                            callback.GetMethodInfo().GetParameterTypeList()));\n                }\n\n                var callbackMethod = callback.GetMethodInfo();\n\n                if (callbackMethod.ReturnType != typeof(void))\n                {\n                    throw new ArgumentException(Resources.InvalidCallbackNotADelegateWithReturnTypeVoid, nameof(callback));\n                }\n\n                if (callbackMethod.GetParameterTypes().Any(Extensions.IsOrContainsTypeMatcher))\n                {\n                    throw new ArgumentException(Resources.TypeMatchersMayNotBeUsedWithCallbacks);\n                }","sourceCodeStart":148,"sourceCodeEnd":184,"githubUrl":"https://github.com/devlooped/moq/blob/89a5be629c752960fe403e95ff583e4ae6f00542/src/Moq/MethodCall.cs#L148-L184","documentation":"SetCallbackBehavior throws this ArgumentException when the callback delegate's parameter types do not match the mocked method's parameter types. Moq validates via CompareParameterTypesTo so the callback receives the exact arguments of the invocation.","triggerScenarios":"`.Callback(lambda)` whose lambda parameters differ in count, order, or type from the mocked method, e.g. mocking `Save(string, int)` with `.Callback((int id) => ...)`.","commonSituations":"Refactorings that changed the mocked method signature without updating Callback lambdas; using a shared helper lambda across setups with different signatures.","solutions":["Update the Callback lambda so its parameter list exactly matches the mocked method's parameters in order and type","If no parameters are needed, use a parameterless `Callback(() => ...)`","Check overload resolution — ensure the setup targets the intended overload"],"exampleFix":"// before\nmock.Setup(m => m.Save(\"key\", 5)).Callback((int id) => log(id));\n// after\nmock.Setup(m => m.Save(\"key\", 5)).Callback((string key, int id) => log(key, id));","handlingStrategy":"validation","validationCode":"var cbParams = callback.GetMethodInfo().GetParameters();\nvar methodParams = method.GetParameters();\nif (cbParams.Length != methodParams.Length) throw new InvalidOperationException(\"Callback parameter count must match mocked method\");","typeGuard":"static bool CallbackParamsMatch(Delegate d, MethodInfo mocked) => d.GetMethodInfo().GetParameters().Select(p => p.ParameterType).SequenceEqual(mocked.GetParameters().Select(p => p.ParameterType));","tryCatchPattern":"try { setup.Callback(cb); } catch (ArgumentException ex) when (ex.Message.Contains(\"callback\")) { /* align callback signature */ }","preventionTips":["Mirror the mocked method's parameter list exactly in Callback lambdas","Use parameterless Callback(() => ...) when arguments aren't needed","Update Callbacks alongside method signature changes"],"tags":["moq","callback","parameter-mismatch","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"89a5be629c752960fe403e95ff583e4ae6f00542","analyzedAt":"2026-09-16T05:31:39.496Z","contentChangedAt":"2026-09-16T05:31:39.496Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}