dotnet/BenchmarkDotNet · error · InvalidBenchmarkDeclarationException
{methodType} method {methodInfo.Name} is generic.\nGeneric {
Error message
{methodType} method {methodInfo.Name} is generic.\nGeneric {methodType} methods are not supported. What it means
Thrown by AssertMethodIsNotGeneric when a benchmark or lifecycle method is a generic method (IsGenericMethod == true). BenchmarkDotNet cannot generate host code for generic methods because the type arguments cannot be determined at code-generation time. Unlike generic types (which BenchmarkSwitcher can handle via type arguments), generic methods are unsupported.
Source
Thrown at src/BenchmarkDotNet/Running/BenchmarkConverter.cs:277
=> 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();
private static object? Map(object? providedValue, Type type)
{
if (providedValue == null)
return null;
if (providedValue.GetType().IsArray)
{
return ArrayParam<IParam>.FromObject(providedValue);
}
// Usually providedValue contains all needed type information,
// but in case of F# enum types in attributes are erased.
// We can to restore them from types of arguments and fields.
// See also:View on GitHub (pinned to b515068b61)
Solutions
- Remove the type parameters and make the method non-generic, using a concrete type.
- If testing multiple types, create separate non-generic benchmark methods for each type.
Example fix
// before
[Benchmark]
public void Run<T>(T[] data) { /* ... */ }
// after
[Benchmark]
public void Run(int[] data) { /* ... */ }
[Benchmark]
public void RunString(string[] data) { /* ... */ } Defensive patterns
Strategy: validation
Validate before calling
var method = typeof(MyBench).GetMethod(nameof(MyBench.Run))!;
if (method.IsGenericMethod)
throw new InvalidOperationException($"{method.Name} is generic and cannot be a benchmark."); Prevention
- Never declare generic benchmark methods; use separate concrete methods per type.
- If testing multiple types, create distinct [Benchmark] methods or benchmark classes.
- Add a validation check in a base test class that scans for generic benchmark methods.
When it happens
Trigger: Declaring [Benchmark] public void Run<T>(T value) or [GlobalSetup] public void Setup<T>() — any method with type parameters annotated with a benchmark attribute.
Common situations: Writing a generic benchmark utility method and annotating it with [Benchmark]. Refactoring a helper into a benchmark method while leaving its generic type parameter.
Related errors
- {type.Name} is generic type definition, use BenchmarkSwitche
- {methodType} method {methodInfo.Name} has incorrect signatur
- {methodType} method {methodInfo.Name} has incorrect access m
- {sourceType.Name} has no public, accessible method/property
- {memberInfo.Name} of type {type.Name} does not implement IEn
AI-assisted analysis of dotnet/BenchmarkDotNet@b515068b61 (2026-08-13).
Data as JSON: /api/errors/8a35dc99ff7557b6.
Report an issue: GitHub.