dotnet/BenchmarkDotNet · error · InvalidBenchmarkDeclarationException

{methodType} method {methodInfo.Name} has incorrect signatur

Error message

{methodType} method {methodInfo.Name} has incorrect signature.\nMethod shouldn't have any arguments.

What it means

Thrown by AssertMethodHasCorrectSignature when a benchmark method (or a related lifecycle method like GlobalSetup/GlobalCleanup) declares parameters but does not have [Arguments] or [ArgumentsSource] attributes. BenchmarkDotNet requires parameterless signatures unless argument-providing attributes are present. The check is: GetParameters().Any() && !HasAttribute<ArgumentsAttribute>() && !HasAttribute<ArgumentsSourceAttribute>().

Source

Thrown at src/BenchmarkDotNet/Running/BenchmarkConverter.cs:264

            if (!benchmark.HasAttribute<ArgumentsSourceAttribute>())
                yield break;

            var argumentsSourceAttribute = benchmark.GetCustomAttribute<ArgumentsSourceAttribute>()!;
            var targetType = argumentsSourceAttribute.Type ?? benchmarkType;

            var valuesInfo = GetValidValuesForParamsSource(targetType, argumentsSourceAttribute.Name);
            for (int sourceIndex = 0; sourceIndex < valuesInfo.values.Length; sourceIndex++)
                yield return SmartParamBuilder.CreateForArguments(benchmark, parameterDefinitions, valuesInfo, sourceIndex, summaryStyle);
        }

        private static ImmutableArray<BenchmarkCase> GetFilteredBenchmarks(IEnumerable<BenchmarkCase> benchmarks, IEnumerable<IFilter> filters)
            => benchmarks.Where(benchmark => filters.All(filter => filter.Predicate(benchmark))).ToImmutableArray();

        private static void AssertMethodHasCorrectSignature(string methodType, MethodInfo methodInfo)
        {
            if (methodInfo.GetParameters().Any() && !methodInfo.HasAttribute<ArgumentsAttribute>() && !methodInfo.HasAttribute<ArgumentsSourceAttribute>())
                throw new InvalidBenchmarkDeclarationException($"{methodType} method {methodInfo.Name} has incorrect signature.\nMethod shouldn't have any arguments.");
        }

        private static void AssertMethodIsAccessible(string methodType, MethodInfo methodInfo)
        {
            if (!methodInfo.IsPublic)
                throw new InvalidBenchmarkDeclarationException($"{methodType} method {methodInfo.Name} has incorrect access modifiers.\nMethod must be public.");
            /* Moved the code that verifies if DeclaringType of a given MethodInfo (a method) is publicly accessible to CompilationValidator */
        }

        private static void AssertMethodIsNotGeneric(string methodType, MethodInfo methodInfo)
        {
            if (methodInfo.IsGenericMethod)
                throw new InvalidBenchmarkDeclarationException($"{methodType} method {methodInfo.Name} is generic.\nGeneric {methodType} methods are not supported.");
        }

        private static object?[] GetValidValues(object?[] values, Type parameterType)
            => values.Select(value => Map(value, parameterType)).ToArray();

View on GitHub (pinned to b515068b61)

Solutions

  1. Remove parameters from the method signature if argument injection is not intended.
  2. Add [Arguments(...)] or [ArgumentsSource(nameof(Source))] to the method if parameters are intentional.
  3. For [GlobalSetup]/[GlobalCleanup], ensure they are parameterless.

Example fix

// before
[Benchmark]
public void Run(int size) { /* ... */ }

// after
[Benchmark]
[Arguments(100)]
public void Run(int size) { /* ... */ }
Defensive patterns

Strategy: validation

Validate before calling

var method = typeof(MyBench).GetMethod(nameof(MyBench.Run))!;
if (method.GetParameters().Any() &&
    !method.IsDefined(typeof(ArgumentsAttribute), false) &&
    !method.IsDefined(typeof(ArgumentsSourceAttribute), false))
    throw new InvalidOperationException("Method has parameters but no [Arguments]/[ArgumentsSource].");

Prevention

When it happens

Trigger: Declaring [Benchmark] public void Run(int x) without adding [Arguments] or [ArgumentsSource]. Also applies to [GlobalSetup], [GlobalCleanup], [IterationSetup], etc. methods that erroneously take parameters.

Common situations: Writing a benchmark method that accidentally takes a parameter (e.g., refactored from a test). Declaring a GlobalSetup method with parameters (which is not supported). Forgetting to add [ArgumentsSource] when parameterizing.

Related errors


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