{"record":{"id":"563693db9575a5e6","repo":"dotnet/BenchmarkDotNet","slug":"the-argument-must-be-an-array","errorCode":null,"errorMessage":"The argument must be an array","messagePattern":"The argument must be an array","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/BenchmarkDotNet/Code/ArrayParam.cs","lineNumber":72,"sourceCode":"        /// <summary>\n        /// for types where calling .ToString() will be enough to re-create them in auto-generated source code file (integers, strings and other primitives)\n        /// </summary>\n        public static ArrayParam<T> ForPrimitives(T[] array) => new ArrayParam<T>(array);\n\n        /// <summary>\n        /// for types where calling .ToString() will be NOT enough to re-create them in auto-generated source code file\n        /// </summary>\n        /// <param name=\"array\">the array</param>\n        /// <param name=\"toSourceCode\">method which transforms an item of type T to it's C# representation\n        /// example: point => $\"new Point2d({point.X}, {point.Y})\"\n        /// </param>\n        [PublicAPI] public static ArrayParam<T> ForComplexTypes(T[] array, Func<T, string> toSourceCode) => new ArrayParam<T>(array, toSourceCode);\n\n        internal static IParam? FromObject(object array)\n        {\n            var type = array.GetType();\n            if (!type.IsArray)\n                throw new InvalidOperationException(\"The argument must be an array\");\n            var elementType = type.GetElementType();\n            if (elementType == null)\n                throw new InvalidOperationException(\"Failed to determine type of array elements\");\n            if (!SourceCodeHelper.IsCompilationTimeConstant(elementType))\n                throw new InvalidOperationException(\"The argument must be an array of primitives\");\n\n            var arrayParamType = typeof(ArrayParam<>).MakeGenericType(elementType);\n\n            var methodInfo = arrayParamType.GetMethod(nameof(ForPrimitives), BindingFlags.Public | BindingFlags.Static)\n                ?? throw new InvalidOperationException($\"{nameof(ForPrimitives)} not found\");\n            return (IParam?)methodInfo.Invoke(null, [array]);\n        }\n    }\n}","sourceCodeStart":54,"sourceCodeEnd":86,"githubUrl":"https://github.com/dotnet/BenchmarkDotNet/blob/b515068b61ad1c9c9aa938b8ece4af1e7d6d85a3/src/BenchmarkDotNet/Code/ArrayParam.cs#L54-L86","documentation":"ArrayParam.FromObject is the reflection entry point used when a benchmark argument or [Params] value is supplied as a boxed object that BDN must render into source code. It requires the object to be an actual array (type.IsArray true). If the object is a List, IEnumerable, a scalar, or any other reference/collection type, IsArray is false and it throws InvalidOperationException.","triggerScenarios":"Passing a non-array (List<T>, IEnumerable<T>, a single value, or a jagged/complex object) where BDN expects an array argument via [Arguments] / [ParamsSource] / global arguments that route through FromObject.","commonSituations":"Using [ParamsSource] that returns a List instead of an array, supplying a single object instead of an array, or a custom IParam implementation that calls FromObject on incompatible input.","solutions":["Convert the collection to an array before it reaches BDN: values.ToArray().","For [ParamsSource], return an IReadOnlyList/array of the element type, not a List of arrays.","Ensure [Arguments] array arguments are declared as actual T[] parameters.","For complex element types, use ArrayParam<T>.ForComplexTypes(arr, toSourceCode) instead of FromObject."],"exampleFix":"// before\npublic IEnumerable<object> Values() => new List<object> { 1, 2, 3 };\n[ParamsSource(nameof(Values))] public int N;\n\n// after\npublic IEnumerable<int> Values() => new[] { 1, 2, 3 };\n[ParamsSource(nameof(Values))] public int N;","handlingStrategy":"type-guard","validationCode":"if (array == null || !array.GetType().IsArray)\n    throw new ArgumentException(\"An array argument is required.\", nameof(array));\nreturn ArrayParam.FromObject(array);","typeGuard":"static bool IsArrayParamCompatible(object? o) => o is not null && o.GetType().IsArray;","tryCatchPattern":"try { return ArrayParam.FromObject(obj); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"must be an array\"))\n{\n    // materialize to an array and retry, or reject the param source\n}","preventionTips":["Return arrays (or element-typed IEnumerables) from [ParamsSource], not List<object> or object collections.","Ensure [Arguments] array parameters are declared as T[].","Convert collections with .ToArray() before they reach BDN."],"tags":["arguments","params","array","reflection","invalidoperation"],"backgroundTag":null,"analyzedSha":"b515068b61ad1c9c9aa938b8ece4af1e7d6d85a3","analyzedAt":"2026-08-13T19:12:24.196Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}