{"record":{"id":"a1494411fd5a4776","repo":"dotnet/wpf","slug":"sr-argument-invalidarraytype","errorCode":null,"errorMessage":"SR.Argument_InvalidArrayType","messagePattern":"SR\\.Argument_InvalidArrayType","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Collections/ObjectModel/WeakReadOnlyCollection.cs","lineNumber":177,"sourceCode":"            IList<T> dlist = CreateDereferencedList();\n            if (array is T[] items)\n            {\n                dlist.CopyTo(items, index);\n            }\n            else\n            {\n                //\n                // Catch the obvious case assignment will fail.\n                // We can found 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                    //ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidArrayType);\n                    throw new ArgumentException(SR.Argument_InvalidArrayType);\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                    //ThrowHelper.ThrowArgumentException(ExceptionResource.Argument_InvalidArrayType);\n                    throw new ArgumentException(SR.Argument_InvalidArrayType);\n                }\n\n                int count = dlist.Count;\n                try\n                {\n                    for (int i = 0; i < count; i++)\n                    {\n                        objects[index++] = dlist[i];","sourceCodeStart":159,"sourceCodeEnd":195,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Collections/ObjectModel/WeakReadOnlyCollection.cs#L159-L195","documentation":"For non-T[] destination arrays the library checks type compatibility: it throws ArgumentException with SR.Argument_InvalidArrayType when neither the array's element type nor T is assignable to the other. E.g., copying WeakReference elements into an int[] cannot work because no conversion exists between the element types.","triggerScenarios":"Calling ICollection.CopyTo with an array whose element type is unrelated to T (e.g., string[] when T is WeakReference).","commonSituations":"Generic copy helpers that pass object[]-like buffers typed incorrectly; refactoring changed T without updating the destination array type.","solutions":["Pass an array whose element type matches T or is assignable from/to T (typically T[] or object[])","Fix the destination array declaration to use the correct element type","Guard with targetType.IsAssignableFrom(sourceType) before calling"],"exampleFix":"// before\nstring[] target = new string[collection.Count];\n((ICollection)collection).CopyTo(target, 0);\n// after\nobject[] target = new object[collection.Count];\n((ICollection)collection).CopyTo(target, 0);","handlingStrategy":"type-guard","validationCode":"var targetType = array.GetType().GetElementType();\nif (!(targetType.IsAssignableFrom(typeof(T)) || typeof(T).IsAssignableFrom(targetType))) throw new ArgumentException(\"Array element type incompatible with collection element type.\");","typeGuard":"bool IsCompatibleArrayType(Array a) { var t = a.GetType().GetElementType(); return t != null && (t.IsAssignableFrom(typeof(T)) || typeof(T).IsAssignableFrom(t)); }","tryCatchPattern":"try { ((ICollection)collection).CopyTo(array, index); } catch (ArgumentException e) when (e.Message.Contains(\"array type\")) { object[] boxed = new object[collection.Count]; ((ICollection)collection).CopyTo(boxed, index); }","preventionTips":["Match the destination element type to the collection's T","Use object[] when copying through the non-generic interface","Add type checks in generic copy utilities"],"tags":["wpf","copyto","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}