{"record":{"id":"c3e1feca73060a8b","repo":"dotnet/wpf","slug":"sr-collection-baddestarray-generaltransformcollection","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/GeneralTransformCollection.cs","lineNumber":417,"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":399,"sourceCodeEnd":435,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Generated/GeneralTransformCollection.cs#L399-L435","documentation":"Thrown by GeneralTransformCollection.CopyTo when an element in the collection cannot be stored into the destination array because its type is not assignable to the array's element type. The original InvalidCastException is preserved as the inner exception.","triggerScenarios":"Calling CopyTo(0, array) where array is, e.g., an object[] pre-typed as RotateTransform[] but the collection contains a GeneralTransform of an incompatible concrete type, or an array with element type that does not accept GeneralTransform instances.","commonSituations":"Copying into a strongly typed subclass array (e.g. ScaleTransform[]) when the collection holds other transform types, or copying through the non-generic ICollection.CopyTo with an object[] from legacy code that is actually typed differently.","solutions":["Ensure the destination array element type is GeneralTransform or a base type that accepts all collection elements","Catch ArgumentException and inspect the inner InvalidCastException for the offending element type","Copy via OfType<T>() filtering if the destination must be a subclass array"],"exampleFix":"// before\nvar scales = new ScaleTransform[collection.Count];\n((ICollection)collection).CopyTo(scales, 0);\n// after\nvar transforms = new GeneralTransform[collection.Count];\n((ICollection)collection).CopyTo(transforms, 0);","handlingStrategy":"validation","validationCode":"var elemType = destination.GetType().GetElementType();\nif (elemType != null && !elemType.IsAssignableFrom(typeof(GeneralTransform)))\n    throw new ArgumentException(\"Element type cannot hold GeneralTransform items\");","typeGuard":"static bool CanHoldTransforms(Array a) => a.GetType().GetElementType()?.IsAssignableFrom(typeof(GeneralTransform)) == true;","tryCatchPattern":"try { ((ICollection)collection).CopyTo(destination, 0); } catch (ArgumentException ex) { var inner = ex.InnerException as InvalidCastException; /* inspect and fix element type */ }","preventionTips":["Copy into GeneralTransform[] rather than subclass arrays","Use OfType<T>() when a subclass array is required","Check Array element type in generic copy helpers"],"tags":["wpf","argumentexception","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"}