{"record":{"id":"2fdedf21b53795b9","repo":"AvaloniaUI/Avalonia","slug":"invalid-array-type","errorCode":null,"errorMessage":"Invalid array type","messagePattern":"Invalid array type","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Collections/AvaloniaList.cs","lineNumber":700,"sourceCode":"            }\n\n            if (array is T[] tArray)\n            {\n                _inner.CopyTo(tArray, index);\n            }\n            else\n            {\n                //\n                // Catch the obvious case assignment will fail.\n                // We can't find all possible problems by doing the check though.\n                // For example, if the element type of the Array is derived from T,\n                // we can't figure out if we can successfully copy the element beforehand.\n                //\n                Type targetType = array.GetType().GetElementType()!;\n                Type sourceType = typeof(T);\n                if (!(targetType.IsAssignableFrom(sourceType) || sourceType.IsAssignableFrom(targetType)))\n                {\n                    throw new ArgumentException(\"Invalid array type\");\n                }\n\n                //\n                // We can't cast array of value type to object[], so we don't support\n                // widening of primitive types here.\n                //\n                if (array is not object?[] objects)\n                {\n                    throw new ArgumentException(\"Invalid array type\");\n                }\n\n                int count = _inner.Count;\n                try\n                {\n                    for (int i = 0; i < count; i++)\n                    {\n                        objects[index++] = _inner[i];\n                    }","sourceCodeStart":682,"sourceCodeEnd":718,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Collections/AvaloniaList.cs#L682-L718","documentation":"ArgumentException ('Invalid array type') thrown by AvaloniaList.ICollection.CopyTo when the destination array's element type is neither assignable from the list's element type T nor vice-versa. The implementation only supports arrays whose element type is type-compatible with T.","triggerScenarios":"Passing an array whose element type is unrelated to T, e.g. copying an AvaloniaList<string> into an int[] or a DateTime[]; element types with no inheritance relationship in either direction.","commonSituations":"Reflection/dynamic code that allocates the destination via a runtime type unrelated to T; mis-typed buffer pools; serializers guessing the element type.","solutions":["Allocate the destination array with the exact element type T: new T[list.Count].","If a base type is required, ensure it is a type T is assignable to (e.g. object[] for reference types).","Validate targetType.IsAssignableFrom(typeof(T)) || typeof(T).IsAssignableFrom(targetType) before copying."],"exampleFix":"// before\nvar arr = new int[list.Count];\n((ICollection)stringList).CopyTo(arr, 0);\n\n// after\nvar arr = new string[list.Count];\n((ICollection)stringList).CopyTo(arr, 0);","handlingStrategy":"type-guard","validationCode":"var targetType = array.GetType().GetElementType();\nif (!(targetType.IsAssignableFrom(typeof(T)) || typeof(T).IsAssignableFrom(targetType))) throw new ArgumentException(\"Incompatible array element type.\");","typeGuard":"static bool IsElementTypeCompatible(Array a) {\n    var t = a.GetType().GetElementType()!;\n    return t.IsAssignableFrom(typeof(T)) || typeof(T).IsAssignableFrom(t);\n}","tryCatchPattern":null,"preventionTips":["Allocate the destination with element type T (new T[list.Count]).","Validate element-type assignability before CopyTo.","For base-type destinations, ensure T is assignable to that base type."],"tags":["avalonia","collections","list","copyto","type-validation"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}