{"record":{"id":"6d7a8be83b4b9669","repo":"dotnet/wpf","slug":"cannot-convert-to-type","errorCode":null,"errorMessage":"Cannot convert to type.","messagePattern":"Cannot convert to type\\.","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/ColorTypeConverter.cs","lineNumber":146,"sourceCode":"        /// <param name=\"destinationType\">\n        /// The Type to convert the value parameter to.\n        /// </param>\n        /// <returns>\n        /// The Type to convert the value parameter to.\n        /// </returns>\n        public\n        override\n        object\n        ConvertTo(\n            ITypeDescriptorContext              context,\n            System.Globalization.CultureInfo    culture,\n            object                              value,\n            Type                                destinationType\n            )\n        {\n            if (!IsSupportedType(destinationType))\n            {\n                throw new NotSupportedException(SR.Converter_ConvertToNotSupported);\n            }\n\n            Color color = (Color)value;\n\n            string colorString;\n\n            if (color.ColorContext == null)\n            {\n                colorString = color.ToString(culture);\n\n                if (colorString.StartsWith(\"sc#\", StringComparison.Ordinal) && colorString.Contains('E'))\n                {\n                    //\n                    // Fix bug 1588888: Serialization produces non-compliant scRGB color string.\n                    //\n                    // Avalon Color.ToString() will use \"R\"oundtrip formatting specifier for scRGB colors,\n                    // which may produce numbers in scientific notation, which is invalid XPS.\n                    //","sourceCodeStart":128,"sourceCodeEnd":164,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/ColorTypeConverter.cs#L128-L164","documentation":"ColorTypeConverter.ConvertTo checks the destinationType with IsSupportedType and throws NotSupportedException ('Cannot convert to type.') for unsupported target types. Only the destination types the XPS serializer needs are allowed.","triggerScenarios":"Calling ConvertTo(color, typeof(string)) or any destination type not whitelisted by IsSupportedType on ColorTypeConverter.","commonSituations":"General string formatting of Color for display/logging, design-time property grids, or generic serialization utilities.","solutions":["Use ColorConverter.ConvertToString for string conversion.","Convert manually: $\"#{color.A:X2}{color.R:X2}{color.G:X2}{color.B:X2}\".","Check the converter's supported destination types before calling ConvertTo.","Catch NotSupportedException and fall back to manual formatting."],"exampleFix":"// before\nstring s = (string)new ColorTypeConverter().ConvertTo(null, null, color, typeof(string));\n// after\nstring s = ColorConverter.ConvertToString(color);","handlingStrategy":"type-guard","validationCode":"if (destinationType != typeof(string) /* only whitelisted types */) throw new NotSupportedException();","typeGuard":"static bool CanConvertTo(Type t) => t == typeof(string); // extend per supported set","tryCatchPattern":"try { return converter.ConvertTo(context, culture, color, destinationType); }\ncatch (NotSupportedException) { return ColorConverter.ConvertToString(color); }","preventionTips":["Use ColorConverter.ConvertToString instead of ColorTypeConverter.ConvertTo.","Restrict converter use to XPS serialization scenarios.","Format colors manually when only display output is needed."],"tags":["wpf","type-converter","not-supported"],"backgroundTag":"unsupported-operation","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"}