{"record":{"id":"3aa6afadadbf7741","repo":"dotnet/BenchmarkDotNet","slug":"extend-a-methodcallexpression-but-got-a","errorCode":null,"errorMessage":"Extend a MethodCallExpression, but got a ","messagePattern":"Extend a MethodCallExpression, but got a ","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/BenchmarkDotNet/Extensions/ReportExtensions.cs","lineNumber":15,"sourceCode":"using BenchmarkDotNet.Mathematics;\nusing BenchmarkDotNet.Reports;\nusing System.Linq.Expressions;\n\nnamespace BenchmarkDotNet.Extensions\n{\n    public static class ReportExtensions\n    {\n        public static BenchmarkReport GetReportFor<T>(this Summary summary, Expression<Action<T>> actionExp)\n        {\n            if (actionExp.Body == null)\n                throw new ArgumentException(\"Extend a an Expression with a valid Body\", nameof(actionExp));\n\n            if (!(actionExp.Body is MethodCallExpression methodExp))\n                throw new ArgumentException(\"Extend a MethodCallExpression, but got a \" + actionExp.Body.GetType().Name, nameof(actionExp));\n\n            return summary.Reports.First(r => r.BenchmarkCase.Descriptor.WorkloadMethod == methodExp.Method);\n        }\n\n        public static IList<Measurement> GetRunsFor<T>(this Summary summary, Expression<Action<T>> actionExp)\n        {\n            return summary.GetReportFor(actionExp).GetResultRuns().ToList();\n        }\n\n        public static Statistics GetStatistics(this IReadOnlyCollection<Measurement> runs)\n        {\n            if (runs.IsEmpty())\n                throw new InvalidOperationException(\"List of measurements contains no elements\");\n            return new Statistics(runs.Select(r => r.GetAverageTime().Nanoseconds));\n        }\n\n        public static Statistics GetStatistics(this IEnumerable<Measurement> runs) =>\n            GetStatistics(runs.ToList());","sourceCodeStart":1,"sourceCodeEnd":33,"githubUrl":"https://github.com/dotnet/BenchmarkDotNet/blob/b515068b61ad1c9c9aa938b8ece4af1e7d6d85a3/src/BenchmarkDotNet/Extensions/ReportExtensions.cs#L1-L33","documentation":"GetReportFor<T> requires the expression body to be a MethodCallExpression so it can read the invoked MethodInfo. It throws ArgumentException with the actual body type name when the lambda is not a direct method invocation (e.g. a property access, field access, arithmetic, or conversion).","triggerScenarios":"Calling summary.GetReportFor(b => b.SomeProperty), summary.GetReportFor(b => b.Field), summary.GetReportFor(b => 1 + 1), or any lambda whose body is not a method call.","commonSituations":"Misunderstanding the API as a generic expression evaluator; passing a property getter or a composed expression instead of a single method-call lambda; refactoring a benchmark from a method to a property and forgetting to update the call.","solutions":["Pass a lambda that directly invokes the benchmark method: summary.GetReportFor(b => b.MyBenchmark()).","Ensure the lambda contains exactly one method call and no wrapping conversions, ternaries, or member accesses.","If you only have a MethodInfo, locate the report manually via summary.Reports.First(r => r.BenchmarkCase.Descriptor.WorkloadMethod == method)."],"exampleFix":"// before\nvar report = summary.GetReportFor(b => b.SomeProperty);\n\n// after\nvar report = summary.GetReportFor(b => b.Run());","handlingStrategy":"type-guard","validationCode":"Expression<Action<T>> expr = b => b.Run();\nif (expr.Body is not MethodCallExpression mce)\n    throw new InvalidOperationException(\"expression body must be a method call\");\nvar report = summary.GetReportFor(expr);","typeGuard":"static bool IsMethodCallExpression<T>(Expression<Action<T>> expr) => expr?.Body is MethodCallExpression;","tryCatchPattern":null,"preventionTips":["Pass a single method-invocation lambda to GetReportFor.","Avoid property/field/arithmetic lambdas with this API.","Use a type guard (is MethodCallExpression) when expressions come from dynamic sources."],"tags":["expression-tree","validation","reports","type-guard"],"backgroundTag":null,"analyzedSha":"b515068b61ad1c9c9aa938b8ece4af1e7d6d85a3","analyzedAt":"2026-08-13T19:12:24.196Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}