{"record":{"id":"84f66f861a0c41b8","repo":"dotnet/wpf","slug":"sr-format-sr-collection-baddestarray-visual3dcollection","errorCode":null,"errorMessage":"SR.Format(SR.Collection_BadDestArray, \"Visual3DCollection\")","messagePattern":"SR\\.Format\\(SR\\.Collection_BadDestArray, \"Visual3DCollection\"\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media3D/Visual3DCollection.cs","lineNumber":174,"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, \"Visual3DCollection\"), e);\n            }\n        }\n\n        /// <summary>\n        ///     Determines if the list contains \"value\"\n        /// </summary>\n        public bool Contains(Visual3D value)\n        {\n            VerifyAPIReadOnly(value);\n\n            return (value != null && (value.InternalVisualParent == _owner));\n        }\n\n        /// <summary>\n        ///     Returns the index of value in the list\n        /// </summary>\n        public int IndexOf(Visual3D value)\n        {","sourceCodeStart":156,"sourceCodeEnd":192,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media3D/Visual3DCollection.cs#L156-L192","documentation":"Visual3DCollection.CopyTo stores elements via Array.SetValue inside a try/catch; an element that cannot be cast to the destination array's element type raises InvalidCastException, which is re-thrown as ArgumentException(SR.Collection_BadDestArray, e). The destination array's element type is not assignment-compatible with Visual3D.","triggerScenarios":"Copying into an array typed as an incompatible element type (e.g. int[], or ModelVisual3D[] when the collection holds other Visual3D subclasses) so that Array.SetValue throws InvalidCastException.","commonSituations":"Copying a collection of mixed Visual3D subclasses into a T[] where T is one specific subclass; reflection-based copy utilities that create a wrongly-typed array.","solutions":["Copy into a Visual3D[] (the collection's exact element type) and cast elements individually afterward","If copying to T[] where T : Visual3D, filter first: collection.OfType<T>().ToArray()","Guard with typeof(Visual3D).IsAssignableFrom(destArrayType) before calling CopyTo"],"exampleFix":"// before\nvar arr = new ModelVisual3D[collection.Count];\ncollection.CopyTo(arr, 0); // throws if any child is not ModelVisual3D\n// after\nvar arr = collection.OfType<ModelVisual3D>().ToArray();","handlingStrategy":"type-guard","validationCode":"if (dest is Visual3D[] v3 && dest.Length >= collection.Count) collection.CopyTo(v3, 0);","typeGuard":"static Visual3D[] SafeSnapshot(Visual3DCollection c) { var a = new Visual3D[c.Count]; c.CopyTo(a, 0); return a; }","tryCatchPattern":"try { collection.CopyTo(arr, 0); } catch (ArgumentException e) { /* arr element type incompatible with Visual3D */ }","preventionTips":["Copy into Visual3D[] — the exact element type — and cast elements afterwards","Use OfType<T>().ToArray() when you need a specific subclass array","Avoid reflection-built destination arrays"],"tags":["wpf","collection","type-mismatch","copyto"],"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"}