AvaloniaUI/Avalonia · error · InvalidCastException
Could not convert list index '{i}' of type '{argument}' to '
Error message
Could not convert list index '{i}' of type '{argument}' to '{type}'. What it means
ReflectionIndexerNode.ConvertIndexes throws InvalidCastException when TypeUtilities.TryConvert fails to convert a binding indexer argument to the indexer's declared parameter type. The node has already found a matching indexer (parameter count is correct) but cannot coerce the supplied string/object argument into the expected index type.
Source
Thrown at src/Avalonia.Base/Data/Core/ExpressionNodes/Reflection/ReflectionIndexerNode.cs:116
if (_getter is not null && _indexes is not null)
SetValue(_getter.Invoke(source, _indexes));
else
ClearValue();
}
private static object?[] ConvertIndexes(ParameterInfo[] indexParameters, IList arguments)
{
var result = new List<object?>();
for (var i = 0; i < indexParameters.Length; i++)
{
var type = indexParameters[i].ParameterType;
var argument = arguments[i];
if (TypeUtilities.TryConvert(type, argument, CultureInfo.InvariantCulture, out var value))
result.Add(value);
else
throw new InvalidCastException(
$"Could not convert list index '{i}' of type '{argument}' to '{type}'.");
}
return result.ToArray();
}
private static bool GetIndexer(Type? type, [NotNullWhen(true)] out MethodInfo? getter, out MethodInfo? setter)
{
getter = setter = null;
if (type is null)
return false;
if (type.IsArray)
{
getter = type.GetMethod("Get");
setter = type.GetMethod("Set");
return getter is not null;View on GitHub (pinned to 11c5427268)
Solutions
- Use an indexer argument whose type is convertible to the indexer parameter type (e.g. an integer for a list indexer).
- If indexing a dictionary, ensure the key matches the dictionary's key type (and the string token parses to it).
- Expose a helper property or method on the source that performs the lookup with the correct types instead of relying on the indexer.
Example fix
// before - IList<int> indexed with a non-int token
{Binding Items[abc]}
// after
{Binding Items[0]} Defensive patterns
Strategy: validation
Validate before calling
// Before binding to an indexer, check the argument converts to the parameter type.
static bool IndexConverts(Type containerType, object arg)
{
var indexer = containerType.GetProperties()
.FirstOrDefault(p => p.GetIndexParameters().Length > 0);
if (indexer is null) return false;
var paramType = indexer.GetIndexParameters()[0].ParameterType;
return Avalonia.Utilities.TypeUtilities.TryConvert(paramType, arg,
CultureInfo.InvariantCulture, out _);
}
if (!IndexConverts(source.GetType(), indexArgument))
throw new InvalidCastException($"Index '{indexArgument}' not convertible to indexer type."); Type guard
static bool CanIndexWith(Type containerType, object arg) =>
IndexConverts(containerType, arg); Try / catch
try { /* evaluate indexer binding */ }
catch (InvalidCastException ex) when (ex.Message.Contains("Could not convert list index"))
{ /* supply a correctly-typed index argument */ } Prevention
- Match the indexer argument type to the indexer parameter type (int for lists, TKey for dictionaries).
- For dynamic indices, validate the value parses before building the path.
- Prefer a helper method/property on the source for non-trivial lookups.
When it happens
Trigger: Binding to an indexer with an argument whose type is not convertible to the indexer parameter type, e.g. indexing a List<T> with a string key when the indexer expects int, or supplying a non-numeric string where an int index is required.
Common situations: Binding like {Binding Items[abc]} where Items is an IList (int indexer); a dictionary indexer where the key type does not match the supplied argument; culture/parse issues converting a string index token.
Related errors
- Property {Name} doesn't have a getter
- Property {Name} doesn't have a setter
- Expected indexer argument.
- Expected '{delimiter}'.
- Expected '{close}'.
AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13).
Data as JSON: /api/errors/f8adbf1a97a57f4f.
Report an issue: GitHub.