{"record":{"id":"c78b521124c18798","repo":"dotnet/BenchmarkDotNet","slug":"method-methodtocall-should-be-instance-method","errorCode":null,"errorMessage":"Method {methodToCall} should be instance method.","messagePattern":"Method (.+?) should be instance method\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/BenchmarkDotNet/Helpers/Reflection.Emit/IlGeneratorCallExtensions.cs","lineNumber":37,"sourceCode":"        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);\n            }\n            else\n            {","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/dotnet/BenchmarkDotNet/blob/b515068b61ad1c9c9aa938b8ece4af1e7d6d85a3/src/BenchmarkDotNet/Helpers/Reflection.Emit/IlGeneratorCallExtensions.cs#L19-L55","documentation":"EmitInstanceCallThisValueOnStack (internal) throws ArgumentException when methodToCall.IsStatic is true-the helper is for instance methods only (the inverse of [69]). The emitter cannot treat a static method as an instance call with a 'this' value on the stack.","triggerScenarios":"The internal emitter is asked to emit an instance call but the supplied MethodInfo is static, i.e. a static method reached an instance-call code path.","commonSituations":"Library codegen bug selecting the wrong emit path for a method (e.g. a static helper reached the instance-call branch); not typically user-facing.","solutions":["Adjust the benchmark so the relevant method is an instance method if the call path expects it.","Upgrade BenchmarkDotNet; report a reproducible issue if it continues."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { runner.Run(); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"should be instance method\")) { /* simplify benchmark, report issue */ }","preventionTips":["Prefer instance benchmark methods on normal classes.","Keep BenchmarkDotNet current.","Report reproducible codegen errors with a minimal repro."],"tags":["il-emit","reflection","internal","codegen"],"backgroundTag":null,"analyzedSha":"b515068b61ad1c9c9aa938b8ece4af1e7d6d85a3","analyzedAt":"2026-08-13T19:12:24.196Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}