{"record":{"id":"07fd8880b08c61f3","repo":"dotnet/BenchmarkDotNet","slug":"methodtype-method-methodinfo-name-has-incorrec-07fd88","errorCode":null,"errorMessage":"{methodType} method {methodInfo.Name} has incorrect access modifiers.\\nMethod must be public.","messagePattern":"(.+?) method (.+?) has incorrect access modifiers\\.\\\\nMethod must be public\\.","errorType":"exception","errorClass":"InvalidBenchmarkDeclarationException","httpStatus":null,"severity":"error","filePath":"src/BenchmarkDotNet/Running/BenchmarkConverter.cs","lineNumber":270,"sourceCode":"\n            var valuesInfo = GetValidValuesForParamsSource(targetType, argumentsSourceAttribute.Name);\n            for (int sourceIndex = 0; sourceIndex < valuesInfo.values.Length; sourceIndex++)\n                yield return SmartParamBuilder.CreateForArguments(benchmark, parameterDefinitions, valuesInfo, sourceIndex, summaryStyle);\n        }\n\n        private static ImmutableArray<BenchmarkCase> GetFilteredBenchmarks(IEnumerable<BenchmarkCase> benchmarks, IEnumerable<IFilter> filters)\n            => benchmarks.Where(benchmark => filters.All(filter => filter.Predicate(benchmark))).ToImmutableArray();\n\n        private static void AssertMethodHasCorrectSignature(string methodType, MethodInfo methodInfo)\n        {\n            if (methodInfo.GetParameters().Any() && !methodInfo.HasAttribute<ArgumentsAttribute>() && !methodInfo.HasAttribute<ArgumentsSourceAttribute>())\n                throw new InvalidBenchmarkDeclarationException($\"{methodType} method {methodInfo.Name} has incorrect signature.\\nMethod shouldn't have any arguments.\");\n        }\n\n        private static void AssertMethodIsAccessible(string methodType, MethodInfo methodInfo)\n        {\n            if (!methodInfo.IsPublic)\n                throw new InvalidBenchmarkDeclarationException($\"{methodType} method {methodInfo.Name} has incorrect access modifiers.\\nMethod must be public.\");\n            /* Moved the code that verifies if DeclaringType of a given MethodInfo (a method) is publicly accessible to CompilationValidator */\n        }\n\n        private static void AssertMethodIsNotGeneric(string methodType, MethodInfo methodInfo)\n        {\n            if (methodInfo.IsGenericMethod)\n                throw new InvalidBenchmarkDeclarationException($\"{methodType} method {methodInfo.Name} is generic.\\nGeneric {methodType} methods are not supported.\");\n        }\n\n        private static object?[] GetValidValues(object?[] values, Type parameterType)\n            => values.Select(value => Map(value, parameterType)).ToArray();\n\n        private static object? Map(object? providedValue, Type type)\n        {\n            if (providedValue == null)\n                return null;\n\n            if (providedValue.GetType().IsArray)","sourceCodeStart":252,"sourceCodeEnd":288,"githubUrl":"https://github.com/dotnet/BenchmarkDotNet/blob/b515068b61ad1c9c9aa938b8ece4af1e7d6d85a3/src/BenchmarkDotNet/Running/BenchmarkConverter.cs#L252-L288","documentation":"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.","triggerScenarios":"Declaring a [Benchmark] method as private, internal, or protected. The same applies to [GlobalSetup], [GlobalCleanup], [IterationSetup], [IterationCleanup] methods.","commonSituations":"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.","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."],"exampleFix":"// before\n[Benchmark]\nprivate void Run() { /* ... */ }\n\n// after\n[Benchmark]\npublic void Run() { /* ... */ }","handlingStrategy":"validation","validationCode":"var method = typeof(MyBench).GetMethod(nameof(MyBench.Run))!;\nif (!method.IsPublic)\n    throw new InvalidOperationException($\"{method.Name} must be public.\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["benchmark-declaration","access-modifiers","benchmark-converter"],"backgroundTag":null,"analyzedSha":"b515068b61ad1c9c9aa938b8ece4af1e7d6d85a3","analyzedAt":"2026-08-13T19:12:24.196Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}