{"record":{"id":"c047fa3ee6988817","repo":"AvaloniaUI/Avalonia","slug":"could-not-convert-s-to-typeof-t","errorCode":null,"errorMessage":"Could not convert '{s}' to {typeof(T)}.","messagePattern":"Could not convert '(.+?)' to (.+?)\\.","errorType":"exception","errorClass":"InvalidCastException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Collections/AvaloniaListConverter.cs","lineNumber":38,"sourceCode":"        public override object? ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object? value)\n        {\n            if (value is not string stringValue)\n                return null;\n\n            var result = new AvaloniaList<T>();\n\n            // TODO: Use StringTokenizer here.\n            var values = stringValue.Split(',');\n\n            foreach (var s in values)\n            {\n                if (TypeUtilities.TryConvert(typeof(T), s, culture, out var v))\n                {\n                    result.Add((T)v!);\n                }\n                else\n                {\n                    throw new InvalidCastException($\"Could not convert '{s}' to {typeof(T)}.\");\n                }\n            }\n\n            return result;\n        }\n    }\n}\n","sourceCodeStart":20,"sourceCodeEnd":46,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Collections/AvaloniaListConverter.cs#L20-L46","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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."],"exampleFix":"// before (XAML): <local:MyList>1,2,foo</local:MyList>  <!-- AvaloniaList<int> -->\n\n// after\n<local:MyList>1,2,3</local:MyList>","handlingStrategy":"try-catch","validationCode":"var tokens = stringValue.Split(',', StringSplitOptions.RemoveEmptyEntries);\nforeach (var t in tokens)\n    if (!TypeUtilities.TryConvert(typeof(T), t.Trim(), culture, out _))\n        throw new FormatException($\"Invalid token '{t}' for {typeof(T)}.\");","typeGuard":null,"tryCatchPattern":"try { var list = (AvaloniaList<T>)converter.ConvertFromString(str); }\ncatch (InvalidCastException ex) { /* report invalid token to user/validation */ }","preventionTips":["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."],"tags":["avalonia","collections","type-converter","xaml","parsing"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}