{"record":{"id":"e5f197dcd18074c8","repo":"dotnet/BenchmarkDotNet","slug":"the-methodtocall-should-have-non-null-declaringtyp","errorCode":null,"errorMessage":"The methodToCall should have non-null DeclaringType.","messagePattern":"The methodToCall should have non-null DeclaringType\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/BenchmarkDotNet/Helpers/Reflection.Emit/IlGeneratorCallExtensions.cs","lineNumber":34,"sourceCode":"            EmitCallCore(ilBuilder, null, methodToCall, argLocals);\n        }\n\n        public static void EmitInstanceCallThisValueOnStack(\n            this ILGenerator ilBuilder,\n            LocalBuilder optionalLocalThis,\n            MethodInfo methodToCall,\n            params LocalBuilder[] argLocals) =>\n            EmitInstanceCallThisValueOnStack(ilBuilder, optionalLocalThis, methodToCall, argLocals, false);\n\n        public static void EmitInstanceCallThisValueOnStack(\n            this ILGenerator ilBuilder,\n            LocalBuilder? optionalLocalThis,\n            MethodInfo methodToCall,\n            IEnumerable<LocalBuilder> argLocals,\n            bool forceDirectCall = false)\n        {\n            if (methodToCall.DeclaringType == null)\n                throw new ArgumentException($\"The {nameof(methodToCall)} should have non-null {nameof(methodToCall.DeclaringType)}.\");\n\n            if (methodToCall.IsStatic)\n                throw new ArgumentException($\"Method {methodToCall} should be instance method.\");\n\n            /*\n                IL_0005: stloc.s 11\n                ...\n                IL_0007: callvirt instance valuetype [mscorlib]System.Runtime.CompilerServices.TaskAwaiter`1<!0> class [mscorlib]System.Threading.Tasks.Task`1<int32>::GetAwaiter()\n                --or-\n                IL_000d: ldloca.s 11\n                IL_000f: call instance !0 valuetype [mscorlib]System.Runtime.CompilerServices.TaskAwaiter`1<int32>::GetResult()\n            */\n            if (methodToCall.DeclaringType.IsValueType)\n            {\n                ArgumentNullException.ThrowIfNull(optionalLocalThis);\n\n                ilBuilder.EmitStloc(optionalLocalThis);\n                EmitCallCore(ilBuilder, optionalLocalThis, methodToCall, argLocals, forceDirectCall);","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/dotnet/BenchmarkDotNet/blob/b515068b61ad1c9c9aa938b8ece4af1e7d6d85a3/src/BenchmarkDotNet/Helpers/Reflection.Emit/IlGeneratorCallExtensions.cs#L16-L52","documentation":"EmitInstanceCallThisValueOnStack (internal) requires the target MethodInfo to have a non-null DeclaringType before it can emit an instance call. It throws ArgumentException when methodToCall.DeclaringType is null. This guards an internal precondition of the IL emitter.","triggerScenarios":"The internal emitter receives a MethodInfo whose DeclaringType is null (e.g. a global method, a dynamically built method without a declaring type, or an unbound generic method), so it cannot emit a correct callvirt/call site.","commonSituations":"Edge cases in benchmark code generation involving dynamic or unbound methods; almost always a library codegen bug, not user config.","solutions":["Reduce the benchmark to a plain instance method on a normal class and retry.","Upgrade BenchmarkDotNet to the latest release.","Report the failing benchmark as a reproducible issue if it persists."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { runner.Run(); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"non-null DeclaringType\")) { /* simplify benchmark, report issue */ }","preventionTips":["Avoid exotic method bindings (global/dynamic methods) in benchmarks.","Upgrade BenchmarkDotNet for codegen fixes.","File reproducible internal emit errors as issues."],"tags":["il-emit","reflection","internal","codegen"],"backgroundTag":null,"analyzedSha":"b515068b61ad1c9c9aa938b8ece4af1e7d6d85a3","analyzedAt":"2026-08-13T19:12:24.196Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}