{"record":{"id":"74fa1b91e579fe3e","repo":"dotnet/wpf","slug":"sr-format-sr-cannotconverttype-typeof-sc-dictionaryentry","errorCode":null,"errorMessage":"SR.Format(SR.CannotConvertType, typeof(SC.DictionaryEntry), elementType)","messagePattern":"SR\\.Format\\(SR\\.CannotConvertType, typeof\\(SC\\.DictionaryEntry\\), elementType\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CharacterMetricsDictionary.cs","lineNumber":201,"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<int, CharacterMetrics> item in this)\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<int, CharacterMetrics> item in this)\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 character code and associated CharacterMetrics to the collection.\n        /// </summary>\n        public void Add(int key, CharacterMetrics value)\n        {\n            SetValue(key, value, failIfExists: true);","sourceCodeStart":183,"sourceCodeEnd":219,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CharacterMetricsDictionary.cs#L183-L219","documentation":"When CopyTo's destination is an Array of some other element type (e.g. object[] is fine, but string[] is not), the library checks elementType.IsAssignableFrom(typeof(DictionaryEntry)). If the element type cannot hold a DictionaryEntry struct, an ArgumentException (CannotConvertType) is thrown naming both types.","triggerScenarios":"Calling ((ICollection)CharacterMetricsDictionary).CopyTo(array, 0) where array is a 1D array of an incompatible element type, such as string[], int[], or a custom unrelated type.","commonSituations":"Refactoring that changed the scratch array type; copy/paste of CopyTo calls between collections with different element types; arrays created generically with wrong T.","solutions":["Use a DictionaryEntry[] array (fast path) or an object[] array.","Otherwise ensure the array's element type is DictionaryEntry or a base type/interface it converts to (object, ValueType, IStructuralEquatable...).","Manually enumerate the dictionary and project entries into your target type instead of using CopyTo."],"exampleFix":"// before\nvar arr = new string[dict.Count];\n((ICollection)dict).CopyTo(arr, 0);\n// after\nvar arr = new DictionaryEntry[dict.Count];\n((ICollection)dict).CopyTo(arr, 0);","handlingStrategy":"validation","validationCode":"Type elementType = array.GetType().GetElementType();\nif (!elementType.IsAssignableFrom(typeof(DictionaryEntry)))\n    array = new DictionaryEntry[dict.Count];","typeGuard":null,"tryCatchPattern":"try { ((ICollection)dict).CopyTo(array, index); }\ncatch (ArgumentException) { /* use DictionaryEntry[] or object[] */ }","preventionTips":["Use DictionaryEntry[] (fast path) or object[] for non-generic CopyTo.","Verify element type compatibility before calling.","Project entries manually for custom element types."],"tags":["wpf","icollection","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-21T21:30:21.729Z"}