dotnet/BenchmarkDotNet · error · InvalidBenchmarkDeclarationException
{methodType} method {methodInfo.Name} has incorrect access m
Error message
{methodType} method {methodInfo.Name} has incorrect access modifiers.\nMethod must be public. What it means
Thrown by AssertMethodIsAccessible when a benchmark or lifecycle method (annotated with [Benchmark], [GlobalSetup], etc.) is not public. BenchmarkDotNet generates a host project that calls these methods, so they must be publicly accessible. The check is simply !methodInfo.IsPublic.
Source
Thrown at src/BenchmarkDotNet/Running/BenchmarkConverter.cs:270
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();
private static object? Map(object? providedValue, Type type)
{
if (providedValue == null)
return null;
if (providedValue.GetType().IsArray)View on GitHub (pinned to b515068b61)
Solutions
- Change the method access modifier to public.
- If the class itself is internal, also make the class public or use InternalsVisibleTo if the host assembly needs it (though public method is still required).
- Audit all [Benchmark], [GlobalSetup], [GlobalCleanup], [IterationSetup], [IterationCleanup] methods for public visibility.
Example fix
// before
[Benchmark]
private void Run() { /* ... */ }
// after
[Benchmark]
public void Run() { /* ... */ } Defensive patterns
Strategy: validation
Validate before calling
var method = typeof(MyBench).GetMethod(nameof(MyBench.Run))!;
if (!method.IsPublic)
throw new InvalidOperationException($"{method.Name} must be public."); Prevention
- Make all [Benchmark] and lifecycle methods public.
- Add a Roslyn analyzer or unit test to verify benchmark methods are public.
- Avoid refactoring benchmark methods to lower visibility.
When it happens
Trigger: Declaring a [Benchmark] method as private, internal, or protected. The same applies to [GlobalSetup], [GlobalCleanup], [IterationSetup], [IterationCleanup] methods.
Common situations: Refactoring and changing a benchmark method's visibility to private/internal. Writing benchmarks in a class with default internal access and forgetting to make the benchmark public. Coming from a testing framework where private test methods are acceptable.
Related errors
- {type.Name} is generic type definition, use BenchmarkSwitche
- {methodType} method {methodInfo.Name} has incorrect signatur
- {methodType} method {methodInfo.Name} is generic.\nGeneric {
- {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/07fd8880b08c61f3.
Report an issue: GitHub.