{"record":{"id":"54bc75b21e24fede","repo":"elsa-workflows/elsa-core","slug":"failed-to-convert-an-object-of-type-sourcetype-to","errorCode":null,"errorMessage":"Failed to convert an object of type {sourceType} to {underlyingTargetType}","messagePattern":"Failed to convert an object of type (.+?) to (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"src/clients/Elsa.Api.Client/Extensions/ObjectConverter.cs","lineNumber":185,"sourceCode":"\n                    foreach (var item in enumerable)\n                    {\n                        var convertedItem = ConvertTo(item, desiredCollectionItemType);\n                        collection.Add(convertedItem);\n                    }\n\n                    return collection;\n                }\n            }\n        }\n\n        try\n        {\n            return Convert.ChangeType(value, underlyingTargetType, CultureInfo.InvariantCulture);\n        }\n        catch (InvalidCastException)\n        {\n            throw new Exception($\"Failed to convert an object of type {sourceType} to {underlyingTargetType}\");\n        }\n    }\n\n    /// <summary>\n    /// Returns true if the specified type is date-like type, false otherwise.\n    /// </summary>\n    private static bool IsDateType(Type type)\n    {\n        var dateTypes = new[]\n        {\n            typeof(DateTime),\n            typeof(DateTimeOffset)\n        };\n\n        return dateTypes.Contains(type);\n    }\n\n    /// <summary>","sourceCodeStart":167,"sourceCodeEnd":203,"githubUrl":"https://github.com/elsa-workflows/elsa-core/blob/fe9217bdfa0e27f0e09e45006eb6898f616e513d/src/clients/Elsa.Api.Client/Extensions/ObjectConverter.cs#L167-L203","documentation":"ObjectConverter.ConvertTo uses Convert.ChangeType for scalar conversions and rethrows InvalidCastException as a generic Exception naming the source and target types. It means the source value cannot be losslessly converted to the requested primitive type.","triggerScenarios":"Converting e.g. a string \"abc\" to int, a DateTime to int, or a complex object to a numeric type via ConvertTo<T>.","commonSituations":"Workflow variable test values or inputs with wrong types compared to property types; culture/number-format issues with decimal strings; schema changes between workflow definition versions.","solutions":["Ensure the source value is convertible (e.g. int.TryParse first) to the target type","Correct the input value or the expected property type","Catch the exception and fall back to a default value"],"exampleFix":"// before\nvar n = ObjectConverter.ConvertTo<int>(\"abc\");\n// after\nvar n = int.TryParse(\"abc\", out var v) ? v : 0;","handlingStrategy":"validation","validationCode":"if (value is string s && !double.TryParse(s, NumberStyles.Any, CultureInfo.InvariantCulture, out _))\n    throw new FormatException($\"'{s}' is not numeric\");","typeGuard":null,"tryCatchPattern":"try { n = ObjectConverter.ConvertTo<int>(value); } catch (Exception) { n = 0; }","preventionTips":["Use TryParse for user/JSON-sourced scalars","Keep workflow input types aligned with property metadata","Use invariant culture for numeric strings"],"tags":["dotnet","conversion","invalid-cast"],"backgroundTag":"type-mismatch","analyzedSha":"fe9217bdfa0e27f0e09e45006eb6898f616e513d","analyzedAt":"2026-09-13T20:32:34.702Z","contentChangedAt":"2026-09-13T20:32:34.702Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}