{"record":{"id":"fd4ad0cd47ea041d","repo":"dotnet/wpf","slug":"sr-collection-baddestarray-drawingcollection","errorCode":null,"errorMessage":"SR.Collection_BadDestArray","messagePattern":"SR\\.Collection_BadDestArray","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/DrawingCollection.cs","lineNumber":429,"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":411,"sourceCodeEnd":447,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/DrawingCollection.cs#L411-L447","documentation":"DrawingCollection's ICollection.CopyTo catches the InvalidCastException thrown by array.SetValue when an element cannot be stored in the destination array, and rethrows it as ArgumentException with SR.Collection_BadDestArray naming the collection type. The destination array's element type must be compatible with Drawing.","triggerScenarios":"Calling ((ICollection)drawingCollection).CopyTo(array, index) with an array whose element type cannot hold Drawing instances (e.g. an ImageSource[] if the elements are not ImageSource, a Brush[], or any unrelated type array).","commonSituations":"Copying a DrawingCollection into an array typed for a related-but-different media type (ImageSource[], Visual[]) after refactoring; generic copy helpers constructing mistyped arrays; interop layers converting collection types.","solutions":["Use a Drawing[] as the destination array.","Use the strongly typed CopyTo(Drawing[] array, int index) overload instead of the ICollection one.","Check array.GetType().GetElementType().IsAssignableFrom(typeof(Drawing)) before copying."],"exampleFix":"// before\nvar dest = new ImageSource[coll.Count];\n((ICollection)coll).CopyTo(dest, 0); // ArgumentException: Collection_BadDestArray\n// after\nvar dest = new Drawing[coll.Count];\ncoll.CopyTo(dest, 0);","handlingStrategy":"type-guard","validationCode":"if (array.GetType().GetElementType() is not Type et || !typeof(Drawing).IsAssignableFrom(et))\n    throw new ArgumentException(\"Destination array element type must be Drawing-compatible.\");","typeGuard":"static bool CanHoldDrawings(Array a) =>\n    a != null && a.Rank == 1 && a.GetType().GetElementType() is Type t && typeof(Drawing).IsAssignableFrom(t);","tryCatchPattern":"try { ((ICollection)coll).CopyTo(dest, 0); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"Collection_BadDestArray\"))\n{ /* reallocate as Drawing[] and retry */ }","preventionTips":["Use the typed CopyTo(Drawing[], int) overload.","Keep destination arrays typed Drawing[] for DrawingCollections.","Verify element-type assignability before generic copy operations."],"tags":["wpf","collection","copyto","invalidcast"],"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-22T01:17:13.364Z"}