{"record":{"id":"8f0596a539958db5","repo":"dotnet/wpf","slug":"sr-cannotconverttype","errorCode":null,"errorMessage":"SR.CannotConvertType","messagePattern":"SR\\.CannotConvertType","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CultureSpecificStringDictionary.cs","lineNumber":170,"sourceCode":"            SC.DictionaryEntry[] typedArray = array as SC.DictionaryEntry[];\n            if (typedArray != null)\n            {\n                // it's an array of the exact type\n                foreach (KeyValuePair<XmlLanguage, string> item in _innerDictionary)\n                {\n                    typedArray[index++] = new SC.DictionaryEntry(item.Key, item.Value);\n                }\n            }\n            else\n            {\n                // it's an array of some other type, e.g., object[]; make sure it's one dimensional\n                if (array.Rank != 1)\n                    throw new ArgumentException(SR.Collection_CopyTo_ArrayCannotBeMultidimensional);\n\n                // make sure the element type is compatible\n                Type elementType = array.GetType().GetElementType();\n                if (!elementType.IsAssignableFrom(typeof(SC.DictionaryEntry)))\n                    throw new ArgumentException(SR.Format(SR.CannotConvertType, typeof(SC.DictionaryEntry), elementType));\n\n                foreach (KeyValuePair<XmlLanguage, string> item in _innerDictionary)\n                {\n                    array.SetValue(new SC.DictionaryEntry(item.Key, item.Value), index++);\n                }\n            }\n        }\n\n        #endregion\n\n        #region IDictionary members\n\n        /// <summary>\n        /// Adds a language and associated string to the collection.\n        /// </summary>\n        public void Add(XmlLanguage key, string value)\n        {\n            _innerDictionary.Add(key, ValidateValue(value));","sourceCodeStart":152,"sourceCodeEnd":188,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CultureSpecificStringDictionary.cs#L152-L188","documentation":"The explicit ICollection.CopyTo throws ArgumentException when the destination Array's element type cannot accept a System.Collections.DictionaryEntry — i.e. elementType.IsAssignableFrom(typeof(DictionaryEntry)) is false (after the array passed the multidimensional check).","triggerScenarios":"Calling (ICollection)dict.CopyTo with an array of an incompatible element type, e.g. new string[dict.Count] or new int[dict.Count], routed through the generic Array branch.","commonSituations":"Copy-pasting copy code that used object[] or DictionaryEntry[] into code with a different array type; interop APIs that hand back typed buffers (string[]); generics that inferred the wrong element type.","solutions":["Use DictionaryEntry[] (or object[]) as the destination for the non-generic CopyTo.","Use the strongly typed CopyTo(KeyValuePair<XmlLanguage,string>[], int) overload instead.","Project entries with LINQ (dict.Select(kv => new DictionaryEntry(kv.Key, kv.Value)).ToArray()) into the type you need."],"exampleFix":"// before\nvar arr = new string[dict.Count];\n((ICollection)dict).CopyTo(arr, 0); // string not assignable from DictionaryEntry\n// after\nvar arr = new DictionaryEntry[dict.Count];\n((ICollection)dict).CopyTo(arr, 0);","handlingStrategy":"type-guard","validationCode":"var elementType = array.GetType().GetElementType();\nif (!elementType.IsAssignableFrom(typeof(DictionaryEntry))) array = new DictionaryEntry[((ICollection)dict).Count];\n((ICollection)dict).CopyTo(array, index);","typeGuard":"static bool CanHoldDictionaryEntries(Array a) => a.Rank == 1 && a.GetType().GetElementType().IsAssignableFrom(typeof(DictionaryEntry));","tryCatchPattern":"try { ((ICollection)dict).CopyTo(array, index); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"CannotConvertType\")) { /* switch to DictionaryEntry[] */ }","preventionTips":["Use DictionaryEntry[] or object[] for non-generic CopyTo.","Prefer the strongly typed generic overload.","Project entries via LINQ into the desired element type."],"tags":["wpf","collection","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-21T21:30:21.729Z"}