{"record":{"id":"61b45afeaa63c069","repo":"elsa-workflows/elsa-core","slug":"failed-to-convert-an-object-of-type-sourcetype-to-61b45a","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":"TypeConversionException","httpStatus":null,"severity":"error","filePath":"src/modules/Elsa.Expressions/Helpers/ObjectConverter.cs","lineNumber":325,"sourceCode":"        try\n        {\n            return Convert.ChangeType(value, underlyingTargetType, CultureInfo.InvariantCulture);\n        }\n        catch (FormatException e)\n        {\n            return ReturnOrThrow(e);\n        }\n        catch (InvalidCastException e)\n        {\n            return ReturnOrThrow(e);\n        }\n\n        object? ReturnOrThrow(Exception e)\n        {\n            if (!strictMode)\n                return targetType.GetDefaultValue(); // Backward compatibility: return default value if strict mode is off.\n\n            throw new TypeConversionException($\"Failed to convert an object of type {sourceType} to {underlyingTargetType}\", value, underlyingTargetType, e);\n        }\n    }\n\n    /// <summary>\n    /// Returns true if the specified type is a date-like type, false otherwise.\n    /// </summary>\n    private static bool IsDateType(Type type)\n    {\n        var dateTypes = new[]\n        {\n            typeof(DateTime), typeof(DateTimeOffset), typeof(DateOnly)\n        };\n\n        return dateTypes.Contains(type);\n    }\n\n    /// <summary>\n    /// Converts any date type to the specified target type.","sourceCodeStart":307,"sourceCodeEnd":343,"githubUrl":"https://github.com/elsa-workflows/elsa-core/blob/fe9217bdfa0e27f0e09e45006eb6898f616e513d/src/modules/Elsa.Expressions/Helpers/ObjectConverter.cs#L307-L343","documentation":"ReturnOrThrow in ObjectConverter.ConvertTo is the generic failure path: when any conversion attempt fails, non-strict mode returns the target type's default value for backward compatibility, but strict mode throws TypeConversionException stating the source type could not be converted to the target type.","triggerScenarios":"Calling ConvertTo (with strictMode on) where the value's type cannot be converted to the underlying target type through any of the converter's supported paths — e.g. converting a non-numeric string to int, or an object to a struct with no conversion.","commonSituations":"Workflow input/variable binding where a user-supplied value has the wrong type; enabling StrictExpressions/strict mode in an environment that previously silently produced defaults.","solutions":["Fix the source value type so it matches the target (e.g. pass \"42\" for int targets, not \"abc\")","Add explicit parsing/validation before conversion (int.TryParse, DateTime.TryParse, etc.)","Disable strict conversion mode if the legacy default-value behavior is acceptable","Inspect the inner exception 'e' captured in the TypeConversionException for the exact failing conversion step"],"exampleFix":"// before\nvar count = (int)ObjectConverter.ConvertTo(input, typeof(int)); // input may be \"abc\"\n// after\nif (!int.TryParse(input?.ToString(), out var count))\n    throw new FormatException(\"Input must be a numeric string\");","handlingStrategy":"try-catch","validationCode":"// pre-validate numeric conversion\nif (targetType == typeof(int) && !int.TryParse(source?.ToString(), out _)) throw new FormatException(\"Not numeric\");","typeGuard":"bool canConvert<T>(object? v) => v is T || (v is string s && typeof(T) != typeof(string) && TryParseable<T>(s));","tryCatchPattern":"try { result = ObjectConverter.ConvertTo(value, targetType); }\ncatch (TypeConversionException ex)\n{ logger.LogError(ex.InnerException, \"Conversion {Source} -> {Target} failed\", sourceType, targetType); result = targetType.GetDefaultValue(); }","preventionTips":["Match input types to target types at workflow design time","Only enable strict mode when inputs are pre-validated","Log TypeConversionException details (they include value and target type)"],"tags":["csharp","type-conversion","strict-mode"],"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"}