{"record":{"id":"ebd81c827670bac3","repo":"dotnet/wpf","slug":"sr-collection-baddestarray-model3dcollection","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/Media3D/Generated/Model3DCollection.cs","lineNumber":427,"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":409,"sourceCodeEnd":445,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media3D/Generated/Model3DCollection.cs#L409-L445","documentation":"Model3DCollection.CopyTo catches InvalidCastException from Array.SetValue while copying elements and rethrows it as ArgumentException(SR.Collection_BadDestArray, e) with the collection type name. This happens when the destination array's element type cannot accept a Model3D (e.g. a non-Model3D array passed through the non-generic ICollection.CopyTo).","triggerScenarios":"Calling the non-generic ICollection.CopyTo with an array of an incompatible element type (e.g. object[] filled with other types is fine, but int[] or Visual[] fails); passing a Model3DCollection's items into a wrongly-typed buffer.","commonSituations":"Legacy non-generic ICollection-based code paths; reflection or COM interop helpers that treat collections as IList and pass loosely typed arrays.","solutions":["Copy into a Model3D[] or a covariantly compatible array (e.g. Model3D[] to object[] with enough capacity).","Verify array element type with typeof(Model3D).IsAssignableFrom(array.GetType().GetElementType()) before CopyTo.","Inspect the inner InvalidCastException in the ArgumentException for the exact type conflict."],"exampleFix":"// before\nvar bad = new Visual3D[collection.Count];\n((ICollection)collection).CopyTo(bad, 0); // InvalidCastException -> Collection_BadDestArray\n// after\nvar good = new Model3D[collection.Count];\ncollection.CopyTo(good, 0);","handlingStrategy":"validation","validationCode":"var et = array.GetType().GetElementType();\nif (et == null || !typeof(Model3D).IsAssignableFrom(et)) throw new ArgumentException(\"Destination array element type must be assignable from Model3D.\");","typeGuard":"bool IsCompatibleDest(Array a) => a?.GetElementType() is Type t && typeof(Model3D).IsAssignableFrom(t);","tryCatchPattern":"try { collection.CopyTo(array, index); } catch (ArgumentException ex) when (ex.InnerException is InvalidCastException) { /* use correctly typed array */ }","preventionTips":["Copy only into Model3D[] (or arrays whose element type derives from Model3D)","Check GetElementType().IsAssignableFrom before non-generic CopyTo","Prefer the generic Model3DCollection.CopyTo over ICollection.CopyTo"],"tags":["wpf","collection","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-22T01:17:13.364Z"}