{"record":{"id":"44652a87110b7c80","repo":"dotnet/wpf","slug":"sr-collection-baddestarray-texteffectcollection","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/TextEffectCollection.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/TextEffectCollection.cs#L399-L435","documentation":"TextEffectCollection.CopyTo throws ArgumentException wrapping InvalidCastException when the destination array cannot accept the elements. Internally it calls array.SetValue for each TextEffect; if the array's element type cannot hold a TextEffect, the runtime throws InvalidCastException which is rethrown as Collection_BadDestArray naming the collection type.","triggerScenarios":"Calling CopyTo on an array whose element type is not TextEffect or a compatible type (e.g. object[] works, but a Shape[] or StreamGeometry[] does not).","commonSituations":"Copying a WPF collection into a mismatched typed array after refactoring element types; passing a shared generic buffer of the wrong type; interop code that assumed all collections were object arrays.","solutions":["Use an array of type TextEffect[] (or a compatible base type) as the destination","Check array.GetType().GetElementType().IsAssignableFrom(typeof(TextEffect)) before calling CopyTo","Verify index plus collection count fits within the array bounds (out-of-range also throws here)"],"exampleFix":"// before\nvar effects = new TextEffectCollection();\nvar bad = new object[0]; // actually must be assignable\neffects.CopyTo(bad, 0);\n// after\nvar effects = new TextEffectCollection();\nvar dest = new TextEffect[effects.Count];\neffects.CopyTo(dest, 0);","handlingStrategy":"validation","validationCode":"if (array == null) throw new ArgumentNullException(nameof(array));\nif (array.GetType().GetElementType() != null && !array.GetType().GetElementType().IsAssignableFrom(typeof(TextEffect)))\n    throw new ArgumentException(\"Destination array element type must be TextEffect-compatible.\");","typeGuard":"static bool CanCopyTo(Array a) => a?.GetType().GetElementType()?.IsAssignableFrom(typeof(TextEffect)) == true;","tryCatchPattern":null,"preventionTips":["Always allocate TextEffect[] for CopyTo targets","Check array bounds: index + collection.Count <= array.Length","Avoid passing shared generic buffers of unrelated types"],"tags":["wpf","collections","argumentexception","type-mismatch"],"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"}