{"record":{"id":"0f696365b1f730af","repo":"dotnet/wpf","slug":"sr-format-sr-collection-baddestarray-this-gettype-name","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/FreezableCollection.cs","lineNumber":496,"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":478,"sourceCodeEnd":514,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/FreezableCollection.cs#L478-L514","documentation":"FreezableCollection<T>.CopyTo throws ArgumentException (with InvalidCastException as inner) when the destination Array's element type cannot hold a T. The collection writes each item via Array.SetValue and converts any InvalidCastException into this ArgumentException, naming the collection type. It exists so that copying to an incompatible array fails with a clear message instead of a raw cast exception.","triggerScenarios":"Calling CopyTo with an array whose element type is not T or a supertype of T, e.g. freezing/casting a FreezableCollection<Rect> and copying into an object[] that later fails on SetValue, or copying into an array of an unrelated type (int[], string[]).","commonSituations":"Legacy non-generic ICollection.CopyTo calls, data-binding or serialization code that allocates Array.CreateInstance with the wrong type, refactoring that changed T but not the consuming CopyTo call site.","solutions":["Allocate the destination array with element type exactly T (or a base type/interface T implements), e.g. new MyFreezable[collection.Count].","Use the generic ICollection<T>.CopyTo or ToArray() instead of the non-generic ICollection.CopyTo.","If the target type differs, copy via LINQ: collection.Cast<TargetType>().ToArray() after verifying convertibility."],"exampleFix":"// before\nArray dest = Array.CreateInstance(typeof(object), collection.Count);\ncollection.CopyTo(dest, 0);\n// after\nvar dest = new MyItemType[collection.Count];\n((ICollection<MyItemType>)collection).CopyTo(dest, 0);","handlingStrategy":"validation","validationCode":"if (dest == null) throw new ArgumentNullException(nameof(dest));\nif (dest.Rank != 1) throw new ArgumentException(\"dest must be a 1-D array\");\nif (dest.Length - index < collection.Count) throw new ArgumentException(\"dest too small\");\n// element type must be assignable from every T\nif (!typeof(T).IsAssignableTo(dest.GetType().GetElementType())) throw new ArgumentException(\"dest element type incompatible\");","typeGuard":"bool IsCompatibleDestArray(Array a) => a != null && a.Rank == 1 && typeof(T).IsAssignableTo(a.GetType().GetElementType());","tryCatchPattern":"try { collection.CopyTo(dest, 0); }\ncatch (ArgumentException ex) when (ex.InnerException is InvalidCastException) { /* reallocate as T[] and retry */ }","preventionTips":["Always use strongly typed T[] destinations","Prefer ICollection<T>.CopyTo or ToArray()","Check Array.Rank and remaining size before copying"],"tags":["wpf","collection","array-copy","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"}