dotnet/BenchmarkDotNet · error · ArgumentException
Method {methodToCall} should be static method.
Error message
Method {methodToCall} should be static method. What it means
IlGeneratorCallExtensions.EmitStaticCall is an internal helper used by BenchmarkDotNet's IL emission for generated benchmark assemblies. It requires the supplied MethodInfo to be static; otherwise it throws ArgumentException. End users do not call it directly-it runs during benchmark assembly code generation.
Source
Thrown at src/BenchmarkDotNet/Helpers/Reflection.Emit/IlGeneratorCallExtensions.cs:14
using System.Reflection;
using System.Reflection.Emit;
namespace BenchmarkDotNet.Helpers.Reflection.Emit
{
internal static class IlGeneratorCallExtensions
{
public static void EmitStaticCall(
this ILGenerator ilBuilder,
MethodInfo methodToCall,
params LocalBuilder[] argLocals)
{
if (!methodToCall.IsStatic)
throw new ArgumentException($"Method {methodToCall} should be static method.");
EmitCallCore(ilBuilder, null, methodToCall, argLocals);
}
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)
{View on GitHub (pinned to b515068b61)
Solutions
- Simplify the benchmark method signature to a common supported shape and re-run.
- Update BenchmarkDotNet to the latest version where codegen edge cases are fixed.
- If reproducible on the current version, file a BenchmarkDotNet issue with the benchmark that triggers code generation.
Defensive patterns
Strategy: try-catch
Try / catch
try { runner.Run(); }
catch (ArgumentException ex) when (ex.Message.Contains("should be static method")) { /* simplify benchmark, report issue */ } Prevention
- Use standard benchmark method shapes supported by the codegen.
- Keep BenchmarkDotNet current to pick up codegen fixes.
- Report reproducible codegen ArgumentExceptions with a minimal benchmark.
When it happens
Trigger: The internal IL emitter is asked to emit a static call (call opcode) for a MethodInfo whose IsStatic is false, i.e. an instance method was supplied where a static method was expected.
Common situations: A mismatch inside BenchmarkDotNet's code generator (e.g. awaiting/calling an instance helper as if static), almost always indicating a library bug rather than user misconfiguration. Encountered with unusual benchmark method shapes (async iterators, ref returns, etc.).
Related errors
- The methodToCall should have non-null DeclaringType.
- Method {methodToCall} should be instance method.
- The optionalLocalThis should be null for static calls
- Can not emit base call for {constructorBuilder}
- The constructorBuilder should have non-null DeclaringType.
AI-assisted analysis of dotnet/BenchmarkDotNet@b515068b61 (2026-08-13).
Data as JSON: /api/errors/f4a37bb0760bb15d.
Report an issue: GitHub.