{"record":{"id":"f8adbf1a97a57f4f","repo":"AvaloniaUI/Avalonia","slug":"could-not-convert-list-index-i-of-type-argum","errorCode":null,"errorMessage":"Could not convert list index '{i}' of type '{argument}' to '{type}'.","messagePattern":"Could not convert list index '(.+?)' of type '(.+?)' to '(.+?)'\\.","errorType":"exception","errorClass":"InvalidCastException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Data/Core/ExpressionNodes/Reflection/ReflectionIndexerNode.cs","lineNumber":116,"sourceCode":"        if (_getter is not null && _indexes is not null)\n            SetValue(_getter.Invoke(source, _indexes));\n        else\n            ClearValue();\n    }\n\n    private static object?[] ConvertIndexes(ParameterInfo[] indexParameters, IList arguments)\n    {\n        var result = new List<object?>();\n\n        for (var i = 0; i < indexParameters.Length; i++)\n        {\n            var type = indexParameters[i].ParameterType;\n            var argument = arguments[i];\n\n            if (TypeUtilities.TryConvert(type, argument, CultureInfo.InvariantCulture, out var value))\n                result.Add(value);\n            else\n                throw new InvalidCastException(\n                    $\"Could not convert list index '{i}' of type '{argument}' to '{type}'.\");\n        }\n\n        return result.ToArray();\n    }\n\n    private static bool GetIndexer(Type? type, [NotNullWhen(true)] out MethodInfo? getter, out MethodInfo? setter)\n    {\n        getter = setter = null;\n\n        if (type is null)\n            return false;\n\n        if (type.IsArray)\n        {\n            getter = type.GetMethod(\"Get\");\n            setter = type.GetMethod(\"Set\");\n            return getter is not null;","sourceCodeStart":98,"sourceCodeEnd":134,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Data/Core/ExpressionNodes/Reflection/ReflectionIndexerNode.cs#L98-L134","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before - IList<int> indexed with a non-int token\n{Binding Items[abc]}\n// after\n{Binding Items[0]}","handlingStrategy":"validation","validationCode":"// Before binding to an indexer, check the argument converts to the parameter type.\nstatic bool IndexConverts(Type containerType, object arg)\n{\n    var indexer = containerType.GetProperties()\n        .FirstOrDefault(p => p.GetIndexParameters().Length > 0);\n    if (indexer is null) return false;\n    var paramType = indexer.GetIndexParameters()[0].ParameterType;\n    return Avalonia.Utilities.TypeUtilities.TryConvert(paramType, arg,\n        CultureInfo.InvariantCulture, out _);\n}\nif (!IndexConverts(source.GetType(), indexArgument))\n    throw new InvalidCastException($\"Index '{indexArgument}' not convertible to indexer type.\");","typeGuard":"static bool CanIndexWith(Type containerType, object arg) =>\n    IndexConverts(containerType, arg);","tryCatchPattern":"try { /* evaluate indexer binding */ }\ncatch (InvalidCastException ex) when (ex.Message.Contains(\"Could not convert list index\"))\n{ /* supply a correctly-typed index argument */ }","preventionTips":["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."],"tags":["data-binding","indexer","reflection","type-conversion"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}