unoplatform/uno · error · NotSupportedException
Conversion to type {0} is not supported
Error message
Conversion to type {0} is not supported What it means
Thrown by XamlTypeTypeConverter.ConvertTo when CanConvertTo returns false for the requested destinationType. The converter only supports converting XamlType to typeof(string); any other destination type (or a non-XamlType source value that falls through to base.ConvertTo) triggers NotSupportedException.
Source
Thrown at src/SourceGenerators/System.Xaml/System.Xaml.Schema/XamlTypeTypeConverter.cs:52
public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof (string);
}
public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
{
return destinationType == typeof (string);
}
public override Object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, Object value)
{
throw new NotSupportedException (String.Format (CultureInfo.InvariantCulture, "Conversion from type {0} is not supported", value != null ? value.GetType () : null));
}
public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (!CanConvertTo (context, destinationType))
throw new NotSupportedException (String.Format (CultureInfo.InvariantCulture, "Conversion to type {0} is not supported", destinationType));
var vctx = (IValueSerializerContext) context;
var lookup = vctx != null ? (INamespacePrefixLookup) vctx.GetService (typeof (INamespacePrefixLookup)) : null;
var xt = value as XamlType;
if (xt != null && destinationType == typeof (string)) {
if (lookup != null)
return new XamlTypeName (xt).ToString (lookup);
else
return xt.UnderlyingType != null ? xt.UnderlyingType.ToString () : xt.ToString ();
}
else
return base.ConvertTo (context, culture, value, destinationType); // it seems it still handles not-supported types (such as int).
}
}
}
View on GitHub (pinned to 0418340488)
Solutions
- Only request conversion to typeof(string) from XamlTypeTypeConverter.
- Check CanConvertTo(context, destinationType) before calling ConvertTo and handle false gracefully.
- If the value is not a XamlType, use a TypeConverter appropriate to the value's actual type.
Example fix
// before
string name = (string)converter.ConvertTo(ctx, culture, xamlType, typeof(string)); // OK
int bad = (int)converter.ConvertTo(ctx, culture, xamlType, typeof(int)); // throws
// after
if (converter.CanConvertTo(ctx, typeof(string)))
name = (string)converter.ConvertTo(ctx, culture, xamlType, typeof(string)); Defensive patterns
Strategy: validation
Validate before calling
if (converter.CanConvertTo(context, destinationType))
return converter.ConvertTo(context, culture, value, destinationType); Type guard
static bool CanConvert(XamlTypeTypeConverter c, ITypeDescriptorContext ctx, Type dest)
=> c.CanConvertTo(ctx, dest); Try / catch
try { return converter.ConvertTo(ctx, culture, value, destType); }
catch (NotSupportedException) { /* handle unsupported target type */ } Prevention
- Always check CanConvertTo before ConvertTo.
- XamlTypeTypeConverter only supports string as a destination type.
When it happens
Trigger: Calling ConvertTo(context, culture, value, typeof(int)) or any destinationType other than string. Or calling ConvertTo with a value that is not a XamlType and a destinationType that base.ConvertTo also cannot handle.
Common situations: Generic XAML serialization pipeline that enumerates target types. Misconfigured binding or markup-extension code that requests conversion to an incompatible type. Code that assumes a TypeConverter can round-trip to any Type.
Related errors
- Conversion from type {0} is not supported
- Cannot cast object '{0}' to string
- instance type '{0}' has no default constructor.
- context
- Value must be non-null string.
AI-assisted analysis of unoplatform/uno@0418340488 (2026-08-13).
Data as JSON: /api/errors/d6e0e438799c1006.
Report an issue: GitHub.