AvaloniaUI/Avalonia · error · InvalidCastException
Could not convert '{s}' to {typeof(T)}.
Error message
Could not convert '{s}' to {typeof(T)}. What it means
InvalidCastException thrown by AvaloniaListConverter.ConvertFrom when TypeUtilities.TryConvert fails to convert one of the comma-separated string tokens to the target element type T. The converter splits the input string on ',' and converts each segment; any unconvertible token aborts the whole conversion.
Source
Thrown at src/Avalonia.Base/Collections/AvaloniaListConverter.cs:38
public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object? value)
{
if (value is not string stringValue)
return null;
var result = new AvaloniaList<T>();
// TODO: Use StringTokenizer here.
var values = stringValue.Split(',');
foreach (var s in values)
{
if (TypeUtilities.TryConvert(typeof(T), s, culture, out var v))
{
result.Add((T)v!);
}
else
{
throw new InvalidCastException($"Could not convert '{s}' to {typeof(T)}.");
}
}
return result;
}
}
}
View on GitHub (pinned to 11c5427268)
Solutions
- Ensure every comma-separated token is a valid representation of T (correct format, locale, enum name).
- Pre-validate/trim tokens before conversion; strip empty segments from trailing commas.
- Provide a TypeConverter for T or register a custom TypeUtilities converter if the default cannot parse your format.
- Catch InvalidCastException at the binding/error boundary and surface a validation message to the user.
Example fix
// before (XAML): <local:MyList>1,2,foo</local:MyList> <!-- AvaloniaList<int> --> // after <local:MyList>1,2,3</local:MyList>
Defensive patterns
Strategy: try-catch
Validate before calling
var tokens = stringValue.Split(',', StringSplitOptions.RemoveEmptyEntries);
foreach (var t in tokens)
if (!TypeUtilities.TryConvert(typeof(T), t.Trim(), culture, out _))
throw new FormatException($"Invalid token '{t}' for {typeof(T)}."); Try / catch
try { var list = (AvaloniaList<T>)converter.ConvertFromString(str); }
catch (InvalidCastException ex) { /* report invalid token to user/validation */ } Prevention
- Validate each comma-separated token parses to T before conversion.
- Trim tokens and drop empty segments to handle stray commas.
- Catch InvalidCastException at validation/binding boundaries.
When it happens
Trigger: XAML/markup or TypeDescriptor code converting a string like '1,2,foo' to an AvaloniaList<int>; passing a value that cannot be parsed by TypeUtilities for T (e.g. non-numeric text for an int list, malformed enums for an enum list).
Common situations: User-authored or data-bound string content feeding a TypeConverter for an AvaloniaList<T>; locale/format mismatches (e.g. decimal separator, enum casing); trailing commas or stray whitespace tokens.
Related errors
- Invalid Cue string "{value}"
- Easing "{e}" was not found in {Namespace} namespace.
- IterationCount can't be a negative number.
- dictionary
- Reset called on collection without reset handler.
AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13).
Data as JSON: /api/errors/c047fa3ee6988817.
Report an issue: GitHub.