{"record":{"id":"fa7fdd794a1fa711","repo":"dotnet/BenchmarkDotNet","slug":"failed-to-determine-type-of-array-elements","errorCode":null,"errorMessage":"Failed to determine type of array elements","messagePattern":"Failed to determine type of array elements","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/BenchmarkDotNet/Code/ArrayParam.cs","lineNumber":75,"sourceCode":"        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":57,"sourceCodeEnd":86,"githubUrl":"https://github.com/dotnet/BenchmarkDotNet/blob/b515068b61ad1c9c9aa938b8ece4af1e7d6d85a3/src/BenchmarkDotNet/Code/ArrayParam.cs#L57-L86","documentation":"Inside ArrayParam.FromObject, after the IsArray check passes, type.GetElementType() is expected to yield the element type. For genuine CLR arrays this is essentially always non-null, so this throw is a defensive guard against exotic or edge-case array-like types where GetElementType returns null (e.g. certain Array-derived types or by-ref-like constructs produced via reflection).","triggerScenarios":"Passing a Type whose IsArray is true but whose GetElementType() returns null, which can occur with non-standard Array subclasses or some reflected/constructed array types; not reachable with ordinary T[] values.","commonSituations":"Custom IParam providers or reflection-based argument plumbing that construct unusual array types, or runtime/CLR edge cases on uncommon targets.","solutions":["Use a concrete, standard array type (int[], string[], etc.) instead of an Array subclass or reflected construct.","If you control the value, materialize it as a normal T[] before passing it in.","Report it as a BDN issue if a plain array still triggers it, including the exact runtime/Type involved."],"exampleFix":"// before\nvar arg = Array.CreateInstance(typeof(int), lengths, lowerBounds); // non-zero-based Array subclass\n\n// after\nvar arg = new int[] { 1, 2, 3 }; // standard zero-based array","handlingStrategy":"type-guard","validationCode":"var t = array.GetType();\nif (!t.IsArray || t.GetElementType() is null)\n    throw new ArgumentException(\"A standard zero-based array is required.\", nameof(array));\nreturn ArrayParam.FromObject(array);","typeGuard":"static bool IsStandardArray(object? o) => o is not null && o.GetType().IsArray && o.GetType().GetElementType() is not null;","tryCatchPattern":"try { return ArrayParam.FromObject(obj); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Failed to determine type\"))\n{\n    // copy into a concrete T[] and retry\n}","preventionTips":["Use concrete zero-based arrays (int[], string[]) instead of Array.CreateInstance with bounds or Array subclasses.","Materialize reflected/constructed arrays into standard T[] before passing to BDN."],"tags":["arguments","array","reflection","edge-case","invalidoperation"],"backgroundTag":null,"analyzedSha":"b515068b61ad1c9c9aa938b8ece4af1e7d6d85a3","analyzedAt":"2026-08-13T19:12:24.196Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}