{"record":{"id":"31e3ef59a98352bb","repo":"dotnet/wpf","slug":"sr-collection-baddestarray-transform3dcollection","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/Transform3DCollection.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/Transform3DCollection.cs#L409-L445","documentation":"CopyTo wraps InvalidCastException and rethrows ArgumentException (SR.Collection_BadDestArray) when an element cannot be stored into the destination array, i.e. the array's element type is not assignment-compatible with Transform3D.","triggerScenarios":"Calling the non-generic ICollection.CopyTo with an array whose element type cannot hold Transform3D (e.g. new Visual3D[3] or new object[3] only works via boxing, but an unrelated type like new string[3] fails), causing SetValue to throw InvalidCastException.","commonSituations":"Legacy ICollection-based code passing arrays typed for another collection's element type; refactoring element types between Visual3D/Transform3D collections; reflection-driven copy utilities.","solutions":["Use an array whose element type is Transform3D (or object), or use the generic CopyTo(Transform3D[], int) overload.","Prefer iterating with foreach into a correctly typed buffer.","Verify the array element type with typeof(Transform3D).IsAssignableFrom(arr.GetType().GetElementType()) before calling."],"exampleFix":"// before\nvar target = new Visual3D[collection.Count];\n((ICollection)collection).CopyTo(target, 0);\n// after\nvar target = new Transform3D[collection.Count];\ncollection.CopyTo(target, 0);","handlingStrategy":"validation","validationCode":"var elemType = array.GetType().GetElementType();\nif (!typeof(Transform3D).IsAssignableFrom(elemType) && elemType != typeof(object))\n    throw new ArgumentException(\"Bad destination array type\");","typeGuard":"bool IsCompatible(Array a) => a != null && (a.GetType().GetElementType() == typeof(Transform3D) || a.GetType().GetElementType() == typeof(object));","tryCatchPattern":"try { ((ICollection)collection).CopyTo(arr, 0); } catch (ArgumentException e) when (e.InnerException is InvalidCastException) { /* reallocate correctly-typed array */ }","preventionTips":["Use Transform3D[] arrays or the generic CopyTo overload","Verify element type compatibility before reflection-driven copies","Avoid sharing destination arrays between differently-typed collections"],"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"}