dotnet/wpf · error · NotSupportedException
Cannot convert to type.
Error message
Cannot convert to type.
What it means
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.
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.
Example fix
// before string s = (string)new ColorTypeConverter().ConvertTo(null, null, color, typeof(string)); // after string s = ColorConverter.ConvertToString(color);
Defensive patterns
Strategy: type-guard
Validate before calling
if (destinationType != typeof(string) /* only whitelisted types */) throw new NotSupportedException();
Type guard
static bool CanConvertTo(Type t) => t == typeof(string); // extend per supported set
Try / catch
try { return converter.ConvertTo(context, culture, color, destinationType); }
catch (NotSupportedException) { return ColorConverter.ConvertToString(color); } Prevention
- Use ColorConverter.ConvertToString instead of ColorTypeConverter.ConvertTo.
- Restrict converter use to XPS serialization scenarios.
- Format colors manually when only display output is needed.
When it happens
Trigger: Calling ConvertTo(color, typeof(string)) or any destination type not whitelisted by IsSupportedType on ColorTypeConverter.
Common situations: General string formatting of Color for display/logging, design-time property grids, or generic serialization utilities.
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
- Cannot convert from type.
- Cannot convert to type.
- SR.Converter_ConvertToNotSupported
- SR.Converter_ConvertToNotSupported
- ArgumentNullException (nameof(targetObject))
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/6d7a8be83b4b9669.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/ColorTypeConverter.cs:146
/// <param name="destinationType">
/// The Type to convert the value parameter to.
/// </param>
/// <returns>
/// The Type to convert the value parameter to.
/// </returns>
public
override
object
ConvertTo(
ITypeDescriptorContext context,
System.Globalization.CultureInfo culture,
object value,
Type destinationType
)
{
if (!IsSupportedType(destinationType))
{
throw new NotSupportedException(SR.Converter_ConvertToNotSupported);
}
Color color = (Color)value;
string colorString;
if (color.ColorContext == null)
{
colorString = color.ToString(culture);
if (colorString.StartsWith("sc#", StringComparison.Ordinal) && colorString.Contains('E'))
{
//
// Fix bug 1588888: Serialization produces non-compliant scRGB color string.
//
// Avalon Color.ToString() will use "R"oundtrip formatting specifier for scRGB colors,
// which may produce numbers in scientific notation, which is invalid XPS.
//View on GitHub (pinned to 81131a70a4)