{"record":{"id":"88a8171f9ee4f879","repo":"dotnet/wpf","slug":"sr-format-sr-collection-baddestarray-this-gettype-name-88a817","errorCode":null,"errorMessage":"SR.Format(SR.Collection_BadDestArray, this.GetType().Name)","messagePattern":"SR\\.Format\\(SR\\.Collection_BadDestArray, this\\.GetType\\(\\)\\.Name\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/VectorCollection.cs","lineNumber":382,"sourceCode":"\n            if (array.Rank != 1)\n            {\n                throw new ArgumentException(SR.Collection_BadRank);\n            }\n\n            // Elsewhere in the collection we throw an AE when the type is\n            // bad so we do it here as well to be consistent\n            try\n            {\n                int count = _collection.Count;\n                for (int i = 0; i < count; i++)\n                {\n                    array.SetValue(_collection[i], index + i);\n                }\n            }\n            catch (InvalidCastException e)\n            {\n                throw new ArgumentException(SR.Format(SR.Collection_BadDestArray, this.GetType().Name), e);\n            }\n        }\n\n        bool ICollection.IsSynchronized\n        {\n            get\n            {\n                ReadPreamble();\n\n                return IsFrozen || Dispatcher != null;\n            }\n        }\n\n        object ICollection.SyncRoot\n        {\n            get\n            {\n                ReadPreamble();","sourceCodeStart":364,"sourceCodeEnd":400,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/VectorCollection.cs#L364-L400","documentation":"VectorCollection.CopyTo writes each element into the destination array with Array.SetValue, which throws InvalidCastException when an element cannot be cast to the array's element type (e.g., copying into a Point[] or string[] instead of Vector[]). CopyTo catches that and rethrows ArgumentException(SR.Format(SR.Collection_BadDestArray, this.GetType().Name)) with the original exception as InnerException, matching BCL collection conventions.","triggerScenarios":"Calling ((ICollection)vectorCollection).CopyTo(array, index) where array's element type is not assignable from Vector (e.g., a Point[], object[] is fine, double[] is not), so array.SetValue fails with InvalidCastException inside the try block.","commonSituations":"Assuming VectorCollection copies to Point arrays like some WPF collections do; generic array-conversion helpers with wrong T; refactors where the element type changed between collection and destination buffer.","solutions":["Copy into an array whose element type is exactly Vector (or a compatible base type such as object)","Check the destination array's element type before calling CopyTo (typeof(Vector).IsAssignableFrom(array.GetType().GetElementType()))","Read the InnerException of the ArgumentException to confirm the original InvalidCastException during diagnostics"],"exampleFix":"// before\nvar dest = new Point[collection.Count];\ncollection.CopyTo(dest, 0);\n// after\nvar dest = new Vector[collection.Count];\ncollection.CopyTo(dest, 0);","handlingStrategy":"type-guard","validationCode":"var elemType = dest?.GetType().GetElementType();\nif (elemType == null || !typeof(Vector).IsAssignableFrom(elemType))\n    throw new ArgumentException($\"Destination array element type must be Vector (got {elemType?.Name}).\");","typeGuard":"static bool IsVectorArray(Array a) => a != null && a.Rank == 1 && typeof(Vector).IsAssignableFrom(a.GetType().GetElementType());","tryCatchPattern":"try { collection.CopyTo(dest, index); }\ncatch (ArgumentException ex) when (ex.InnerException is InvalidCastException)\n{ /* dest element type incompatible; allocate Vector[] and retry */ }","preventionTips":["Declare copy destinations as Vector[] explicitly","Remember VectorCollection copies Vectors, unlike PointCollection which copies Points","Inspect InnerException (InvalidCastException) when diagnosing CopyTo ArgumentExceptions"],"tags":["wpf","collections","copyto","type-mismatch"],"backgroundTag":"incompatible-source-type","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"}