{"record":{"id":"c34b51d243e02990","repo":"devlooped/moq","slug":"resources-typematchersmaynotbeusedwithcallbacks","errorCode":null,"errorMessage":"Resources.TypeMatchersMayNotBeUsedWithCallbacks","messagePattern":"Resources\\.TypeMatchersMayNotBeUsedWithCallbacks","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Moq/MethodCall.cs","lineNumber":183,"sourceCode":"                {\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                }\n\n                behavior = new Callback(invocation => callback.InvokePreserveStack(invocation.Arguments));\n            }\n        }\n\n        public void SetFailMessage(string failMessage)\n        {\n            this.failMessage = failMessage;\n        }\n\n        public void SetRaiseEventBehavior<TMock>(Action<TMock> eventExpression, Delegate func)\n            where TMock : class\n        {\n            Guard.NotNull(eventExpression, nameof(eventExpression));\n\n            var expression = ExpressionReconstructor.Instance.ReconstructExpression(eventExpression, this.Mock.ConstructorArguments);\n","sourceCodeStart":165,"sourceCodeEnd":201,"githubUrl":"https://github.com/devlooped/moq/blob/89a5be629c752960fe403e95ff583e4ae6f00542/src/Moq/MethodCall.cs#L165-L201","documentation":"SetCallbackBehavior throws this ArgumentException when a callback delegate's parameter types contain a Moq type matcher (It.IsAny<T>, It.Is<T>, It.Ref). Type matchers are only meaningful in setup argument expressions, not inside callbacks, which receive concrete invocation arguments.","triggerScenarios":"`.Callback((It.IsAny<int> x) => ...)` or passing a delegate whose signature uses Moq matcher types as callback parameters.","commonSituations":"Copy-pasting the setup's matcher expression into the Callback lambda signature; misunderstanding that callbacks receive actual runtime values.","solutions":["Replace matcher types in the callback signature with the concrete parameter types","Use It matchers only in the setup argument list, before the callback","Filter within the callback body with plain if-conditions instead of matchers"],"exampleFix":"// before\nmock.Setup(m => m.Save(It.IsAny<string>())).Callback((It.IsAny<string> s) => log(s));\n// after\nmock.Setup(m => m.Save(It.IsAny<string>())).Callback((string s) => log(s));","handlingStrategy":"type-guard","validationCode":"foreach (var p in callback.GetMethodInfo().GetParameters())\n  if (p.ParameterType.FullName?.StartsWith(\"Moq.It\") == true) throw new InvalidOperationException(\"Type matchers not allowed in callback params\");","typeGuard":"static bool HasNoTypeMatchers(Delegate d) => !d.GetMethodInfo().GetParameters().Any(p => p.ParameterType.IsGenericType && p.ParameterType.GetGenericTypeDefinition().Namespace == \"Moq\");","tryCatchPattern":"try { setup.Callback(cb); } catch (ArgumentException ex) when (ex.Message.Contains(\"Type matchers\")) { /* use concrete types in callback signature */ }","preventionTips":["Use It.* only in setup argument expressions","Declare callback params with concrete types","Never copy matcher types into lambda signatures"],"tags":["moq","type-matcher","callback","invalid-usage"],"backgroundTag":"invalid-argument-value","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"}