{"record":{"id":"c7694728c4504f4d","repo":"dotnet/wpf","slug":"sr-format-sr-collection-baddestarray-this-gettype-name-c76947","errorCode":null,"errorMessage":"SR.Format(SR.Collection_BadDestArray, this.GetType().Name)","messagePattern":"SR\\.Format\\(SR\\.Collection_BadDestArray, this\\.GetType\\(\\)\\.Name\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Generated/TextDecorationCollection.cs","lineNumber":408,"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":390,"sourceCodeEnd":426,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Generated/TextDecorationCollection.cs#L390-L426","documentation":"CopyTo catches the InvalidCastException raised while copying elements into the destination array and rethrows ArgumentException with SR.Collection_BadDestArray, wrapping the original exception. This means the destination array's element type cannot hold a TextDecoration.","triggerScenarios":"Calling ICollection.CopyTo with an array typed to something incompatible, e.g. string[] or non-covariant element types, so Array.SetValue throws InvalidCastException.","commonSituations":"Copying into object[] is fine, but copying into string[]/int[] fails; reflection-based or serialization code that allocates arrays from the wrong element type; IEnumerable.CopyTo generic helpers.","solutions":["Allocate the destination array as TextDecoration[] or object[]","Verify array.GetType().GetElementType().IsAssignableFrom(typeof(TextDecoration)) before CopyTo","Use Cast<TextDecoration>().ToArray() (LINQ) instead of CopyTo for flexible conversion"],"exampleFix":"// before\nvar arr = new string[collection.Count];\n((ICollection)collection).CopyTo(arr, 0);\n// after\nvar arr = new TextDecoration[collection.Count];\ncollection.CopyTo(arr, 0);","handlingStrategy":"type-guard","validationCode":"if (array == null) throw new ArgumentNullException(nameof(array));\nvar elemType = array.GetType().GetElementType();\nif (elemType == null || !elemType.IsAssignableFrom(typeof(TextDecoration))) throw new ArgumentException($\"Array element type {elemType} cannot hold TextDecoration\");","typeGuard":"bool IsCompatibleDestArray(Array a) => a is TextDecoration[] || a is object[] || (a != null && a.GetType().GetElementType()?.IsAssignableFrom(typeof(TextDecoration)) == true);","tryCatchPattern":null,"preventionTips":["Prefer strongly typed TextDecoration[] destinations","Avoid ICollection.CopyTo with arbitrary array types","Use LINQ ToArray/Cast for conversion instead of CopyTo"],"tags":["wpf","argumentexception","type-mismatch","copyto"],"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"}