dotnet/BenchmarkDotNet · error · ArgumentException

Method {methodToCall} should be instance method.

Error message

Method {methodToCall} should be instance method.

What it means

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.

Source

Thrown at src/BenchmarkDotNet/Helpers/Reflection.Emit/IlGeneratorCallExtensions.cs:37

        public static void EmitInstanceCallThisValueOnStack(
            this ILGenerator ilBuilder,
            LocalBuilder optionalLocalThis,
            MethodInfo methodToCall,
            params LocalBuilder[] argLocals) =>
            EmitInstanceCallThisValueOnStack(ilBuilder, optionalLocalThis, methodToCall, argLocals, false);

        public static void EmitInstanceCallThisValueOnStack(
            this ILGenerator ilBuilder,
            LocalBuilder? optionalLocalThis,
            MethodInfo methodToCall,
            IEnumerable<LocalBuilder> argLocals,
            bool forceDirectCall = false)
        {
            if (methodToCall.DeclaringType == null)
                throw new ArgumentException($"The {nameof(methodToCall)} should have non-null {nameof(methodToCall.DeclaringType)}.");

            if (methodToCall.IsStatic)
                throw new ArgumentException($"Method {methodToCall} should be instance method.");

            /*
                IL_0005: stloc.s 11
                ...
                IL_0007: callvirt instance valuetype [mscorlib]System.Runtime.CompilerServices.TaskAwaiter`1<!0> class [mscorlib]System.Threading.Tasks.Task`1<int32>::GetAwaiter()
                --or-
                IL_000d: ldloca.s 11
                IL_000f: call instance !0 valuetype [mscorlib]System.Runtime.CompilerServices.TaskAwaiter`1<int32>::GetResult()
            */
            if (methodToCall.DeclaringType.IsValueType)
            {
                ArgumentNullException.ThrowIfNull(optionalLocalThis);

                ilBuilder.EmitStloc(optionalLocalThis);
                EmitCallCore(ilBuilder, optionalLocalThis, methodToCall, argLocals, forceDirectCall);
            }
            else
            {

View on GitHub (pinned to b515068b61)

Solutions

  1. Adjust the benchmark so the relevant method is an instance method if the call path expects it.
  2. Upgrade BenchmarkDotNet; report a reproducible issue if it continues.
Defensive patterns

Strategy: try-catch

Try / catch

try { runner.Run(); }
catch (ArgumentException ex) when (ex.Message.Contains("should be instance method")) { /* simplify benchmark, report issue */ }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of dotnet/BenchmarkDotNet@b515068b61 (2026-08-13). Data as JSON: /api/errors/c78b521124c18798. Report an issue: GitHub.