{"record":{"id":"4f2a11b94efc7cf1","repo":"OrchardCMS/OrchardCore","slug":"cannot-convert-this-to-conversiontype","errorCode":null,"errorMessage":"Cannot convert {this} to {conversionType}","messagePattern":"Cannot convert (.+?) to (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/OrchardCore/OrchardCore.Abstractions/Json/Dynamic/JsonDynamicValue.cs","lineNumber":233,"sourceCode":"    sbyte IConvertible.ToSByte(IFormatProvider? provider)\n    {\n        return (sbyte)this;\n    }\n\n    float IConvertible.ToSingle(IFormatProvider? provider)\n    {\n        return (float)this;\n    }\n\n    string IConvertible.ToString(IFormatProvider? provider)\n    {\n        return ToString(provider);\n    }\n\n    object IConvertible.ToType(Type conversionType, IFormatProvider? provider)\n    {\n        return _jsonValue?.ToObject(conversionType)\n            ?? throw new InvalidOperationException($\"Cannot convert {this} to {conversionType}\");\n    }\n\n    ushort IConvertible.ToUInt16(IFormatProvider? provider)\n    {\n        return (ushort)this;\n    }\n\n    uint IConvertible.ToUInt32(IFormatProvider? provider)\n    {\n        return (uint)this;\n    }\n\n    ulong IConvertible.ToUInt64(IFormatProvider? provider)\n    {\n        return (ulong)this;\n    }\n\n    public static bool operator ==(JsonDynamicValue left, JsonDynamicValue right)","sourceCodeStart":215,"sourceCodeEnd":251,"githubUrl":"https://github.com/OrchardCMS/OrchardCore/blob/4306c0717fe573f6fca1b4955909ddab6a192807/src/OrchardCore/OrchardCore.Abstractions/Json/Dynamic/JsonDynamicValue.cs#L215-L251","documentation":"JsonDynamicValue implements IConvertible; when ToType is called the underlying JValue is converted with ToObject(conversionType). If the JSON value cannot be converted to the requested type (or the value is null), an InvalidOperationException with 'Cannot convert ...' is thrown.","triggerScenarios":"Calling Convert.ChangeType(dynamicJsonValue, someType) or another IConvertible.ToType path where the JValue's runtime JSON type is incompatible with conversionType — e.g. converting a JSON string \"abc\" to int, an object/array to a primitive, or a null value to any type.","commonSituations":"Dynamic content-field values whose shape changed between data versions; recipes importing strings that were assumed numeric; querying dynamic documents and casting values with Convert.ChangeType.","solutions":["Check the underlying JSON token type (JsonValue.Type) before converting and branch on it.","Use ToString(provider) plus type-specific parsing (int.TryParse, DateTime.Parse with culture) for resilient conversion.","Handle null dynamic values explicitly before invoking conversion.","Catch InvalidOperationException and fall back to a default value appropriate for the field."],"exampleFix":"// before\nvar count = (int)Convert.ChangeType(jsonValue, typeof(int));\n// after\nvar raw = jsonValue?.ToString();\nvar count = int.TryParse(raw, NumberStyles.Any, CultureInfo.InvariantCulture, out var c) ? c : 0;","handlingStrategy":"type-guard","validationCode":"// Confirm the underlying token supports the target conversion\nbool CanConvertTo(JsonDynamicValue v, Type t) =>\n    v?._jsonValue != null &&\n    !(t.IsPrimitive && (v._jsonValue.Type == JTokenType.Object || v._jsonValue.Type == JTokenType.Array));","typeGuard":"static bool IsConvertible<T>(JsonDynamicValue v, out T result)\n{\n    try { result = (T)Convert.ChangeType(v?.ToString(), typeof(T), CultureInfo.InvariantCulture); return true; }\n    catch { result = default; return false; }\n}","tryCatchPattern":"try\n{\n    var converted = Convert.ChangeType(jsonValue, conversionType, CultureInfo.InvariantCulture);\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Cannot convert\"))\n{\n    // fall back to string parsing or a default value\n}","preventionTips":["Prefer ToString + TryParse over IConvertible.ToType for dynamic JSON.","Check the JValue token type before converting primitives.","Handle null dynamic values explicitly before conversion."],"tags":["json","dynamic","type-conversion"],"backgroundTag":"type-mismatch","analyzedSha":"4306c0717fe573f6fca1b4955909ddab6a192807","analyzedAt":"2026-09-13T17:41:05.024Z","contentChangedAt":"2026-09-13T17:41:05.024Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}