{"record":{"id":"2956c33d08c417ee","repo":"elsa-workflows/elsa-core","slug":"invalid-value-type-objectconverter","errorCode":null,"errorMessage":"Invalid value type.","messagePattern":"Invalid value type\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/modules/Elsa.Expressions/Helpers/ObjectConverter.cs","lineNumber":358,"sourceCode":"    }\n\n    /// <summary>\n    /// Converts any date type to the specified target type.\n    /// </summary>\n    /// <param name=\"value\">Any of <see cref=\"DateTime\"/>, <see cref=\"DateTimeOffset\"/> or <see cref=\"DateOnly\"/>.</param>\n    /// <param name=\"targetType\">Any of <see cref=\"DateTime\"/>, <see cref=\"DateTimeOffset\"/> or <see cref=\"DateOnly\"/>.</param>\n    /// <returns>The converted value.</returns>\n    /// <exception cref=\"ArgumentException\">Thrown if <paramref name=\"value\"/> is not of type <see cref=\"DateTime\"/>, <see cref=\"DateTimeOffset\"/> or <see cref=\"DateOnly\"/>.</exception>\n    private static object ConvertAnyDateType(object value, Type targetType)\n    {\n        return targetType switch\n        {\n            { } t when t == typeof(DateTime) => value switch\n            {\n                DateTime dateTime => dateTime,\n                DateTimeOffset dateTimeOffset => dateTimeOffset.DateTime,\n                DateOnly date => new(date.Year, date.Month, date.Day),\n                _ => throw new ArgumentException(\"Invalid value type.\")\n            },\n            { } t when t == typeof(DateTimeOffset) => value switch\n            {\n                DateTime dateTime => new(dateTime),\n                DateTimeOffset dateTimeOffset => dateTimeOffset,\n                DateOnly date => new(date.Year, date.Month, date.Day, 0, 0, 0, TimeSpan.Zero),\n                _ => throw new ArgumentException(\"Invalid value type.\")\n            },\n            { } t when t == typeof(DateOnly) => value switch\n            {\n                DateTime dateTime => new(dateTime.Year, dateTime.Month, dateTime.Day),\n                DateTimeOffset dateTimeOffset => new(dateTimeOffset.Year, dateTimeOffset.Month, dateTimeOffset.Day),\n                DateOnly date => date,\n                _ => throw new ArgumentException(\"Invalid value type.\")\n            },\n            _ => throw new ArgumentException(\"Invalid target type.\")\n        };\n    }","sourceCodeStart":340,"sourceCodeEnd":376,"githubUrl":"https://github.com/elsa-workflows/elsa-core/blob/fe9217bdfa0e27f0e09e45006eb6898f616e513d/src/modules/Elsa.Expressions/Helpers/ObjectConverter.cs#L340-L376","documentation":"ConvertAnyDateType handles DateTime targets: it accepts DateTime, DateTimeOffset, and DateOnly source values and throws ArgumentException \"Invalid value type.\" for anything else. This prevents silently converting incompatible values (e.g. strings that were not pre-parsed) into DateTime.","triggerScenarios":"Calling ConvertTo with a target of typeof(DateTime) while the value is not DateTime, DateTimeOffset, or DateOnly — e.g. a raw string, long timestamp, or TimeOnly value.","commonSituations":"Workflow inputs as ISO-8601 strings fed into DateTime-typed variables without prior parsing, or passing unix timestamps expecting automatic conversion.","solutions":["Pre-parse strings to DateTime before conversion: DateTime.Parse(value, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind)","Convert numeric timestamps explicitly with DateTimeOffset.FromUnixTimeSeconds/Milliseconds","Pass DateOnly/DateTimeOffset/DateTime typed values instead of strings where possible","Catch ArgumentException and return a validation error to the user of the workflow"],"exampleFix":"// before\nvar date = (DateTime)ObjectConverter.ConvertTo(\"2024-01-01\", typeof(DateTime));\n// after\nvar date = DateTime.Parse(\"2024-01-01\", CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind);\nvar result = ObjectConverter.ConvertTo(date, typeof(DateTime));","handlingStrategy":"validation","validationCode":"bool isDateConvertible(object? v) => v is DateTime or DateTimeOffset or DateOnly;","typeGuard":"bool IsDateTimeSource(object? v) => v is DateTime or DateTimeOffset or DateOnly;","tryCatchPattern":"try { d = (DateTime)ObjectConverter.ConvertTo(v, typeof(DateTime)); }\ncatch (ArgumentException ex) when (ex.Message == \"Invalid value type.\") { d = DateTime.MinValue; }","preventionTips":["Parse date strings before passing them to ConvertTo","Use RoundtripKind with InvariantCulture for ISO-8601","Keep date inputs typed (DateTime/DateOnly) in workflow models"],"tags":["csharp","datetime","type-conversion"],"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"}