dotnet/wpf · error · NotSupportedException
Cannot convert from type.
Error message
Cannot convert from type.
What it means
ColorTypeConverter.ConvertFrom only supports the source types it recognizes (via IsSupportedType). Passing a value whose type is not supported throws NotSupportedException with the message 'Cannot convert from type.' The converter is intentionally minimal for XPS serialization.
Solutions
- Use ColorConverter (System.Windows.Media) instead of ColorTypeConverter to parse color strings.
- Only pass types accepted by IsSupportedType to ConvertFrom.
- Parse colors manually with ColorConverter.ConvertFromString.
- Catch NotSupportedException and fall back to a real color parser.
Example fix
// before
var color = (Color)new ColorTypeConverter().ConvertFromString("#FF0000");
// after
var color = (Color)ColorConverter.ConvertFromString("#FF0000"); Defensive patterns
Strategy: try-catch
Validate before calling
if (value == null || !value.GetType().IsInstanceOfType(typeof(Color))) // verify supported input before ConvertFrom colorTypeConverter.ConvertFrom(null, null, value);
Type guard
static bool CanConvertFrom(object v) => v is Color;
Try / catch
try { return converter.ConvertFrom(context, culture, value); }
catch (NotSupportedException) { return ColorConverter.ConvertFromString(value.ToString()); } Prevention
- Use ColorConverter for string-based color parsing, not ColorTypeConverter.
- Never treat ReachFramework serialization converters as general-purpose.
- Check IsSupportedType behavior before calling ConvertFrom.
When it happens
Trigger: Calling TypeConverter.ConvertFrom on ColorTypeConverter with a string or other type it does not list in IsSupportedType.
Common situations: Using ColorTypeConverter as a general-purpose Color parser in XAML or data binding; expecting ConvertFrom to parse hex color strings.
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 to 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/5121133376ce6772.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/ColorTypeConverter.cs:108
/// <param name="value">
/// The Object to convert.
/// </param>
/// <returns>
/// An Object that represents the converted value.
/// </returns>
public
override
object
ConvertFrom(
ITypeDescriptorContext context,
System.Globalization.CultureInfo culture,
object value
)
{
ArgumentNullException.ThrowIfNull(value);
if (!IsSupportedType(value.GetType()))
{
throw new NotSupportedException(SR.Converter_ConvertFromNotSupported);
}
throw new NotImplementedException();
}
/// <summary>
/// Converts the given value object to the specified type,
/// using the arguments.
/// </summary>
/// <param name="context">
/// An ITypeDescriptorContext that provides a format context.
/// </param>
/// <param name="culture">
/// A CultureInfo object. If null is passed, the current
/// culture is assumed.
/// </param>
/// <param name="value">
/// The Object to convert.View on GitHub (pinned to 81131a70a4)