{"record":{"id":"6a36452dbc1b4fa1","repo":"microsoft/aspire","slug":"cannot-cast-paramname-from-value-gettype-tostring-null-to-6a3645","errorCode":null,"errorMessage":"Cannot cast '{paramName}' from '{value?.GetType().ToString() ?? \"null\"}' to '{typeof(T)}'","messagePattern":"Cannot cast '(.+?)' from '(.+?)' to '(.+?)'","errorType":"validation","errorClass":"InvalidCastException","httpStatus":null,"severity":"error","filePath":"src/Vendoring/OpenTelemetry.Instrumentation.StackExchangeRedis/Shared/Guard.cs","lineNumber":209,"sourceCode":"        [MethodImpl(MethodImplOptions.AggressiveInlining)]\n        public static void ThrowIfOutOfRange(double value, [CallerArgumentExpression(nameof(value))] string? paramName = null, double min = double.MinValue, double max = double.MaxValue, string? minName = null, string? maxName = null, string? message = null)\n        {\n            Range(value, paramName, min, max, minName, maxName, message);\n        }\n\n        /// <summary>\n        /// Throw an exception if the value is not of the expected type.\n        /// </summary>\n        /// <param name=\"value\">The value to check.</param>\n        /// <param name=\"paramName\">The parameter name to use in the thrown exception.</param>\n        /// <typeparam name=\"T\">The type attempted to convert to.</typeparam>\n        /// <returns>The value casted to the specified type.</returns>\n        [DebuggerHidden]\n        [MethodImpl(MethodImplOptions.AggressiveInlining)]\n        public static T ThrowIfNotOfType<T>([NotNull] object? value, [CallerArgumentExpression(nameof(value))] string? paramName = null)\n        {\n            return value is not T result\n                ? throw new InvalidCastException($\"Cannot cast '{paramName}' from '{value?.GetType().ToString() ?? \"null\"}' to '{typeof(T)}'\")\n                : result;\n        }\n\n        [DebuggerHidden]\n        [MethodImpl(MethodImplOptions.AggressiveInlining)]\n        private static void Range<T>(T value, string? paramName, T min, T max, string? minName, string? maxName, string? message)\n            where T : IComparable<T>\n        {\n            if (value.CompareTo(min) < 0 || value.CompareTo(max) > 0)\n            {\n                var minMessage = minName != null ? $\": {minName}\" : string.Empty;\n                var maxMessage = maxName != null ? $\": {maxName}\" : string.Empty;\n                var exMessage = message ?? string.Format(\n                    CultureInfo.InvariantCulture,\n                    \"Must be in the range: [{0}{1}, {2}{3}]\",\n                    min,\n                    minMessage,\n                    max,","sourceCodeStart":191,"sourceCodeEnd":227,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Vendoring/OpenTelemetry.Instrumentation.StackExchangeRedis/Shared/Guard.cs#L191-L227","documentation":"Guard.ThrowIfNotOfType<T> performs a cast and throws InvalidCastException when the supplied object is null or not assignable to T, with a message showing the parameter name, the runtime type, and the target type. It is used where the vendored instrumentation expects a strongly-typed payload (e.g. an object box previously stored).","triggerScenarios":"Calling Guard.ThrowIfNotOfType<T>(value) where value is null or of an unrelated type; typically when retrieving cached/state objects that were stored under a different type.","commonSituations":"Two different components storing different types under the same key; an object changed type after a library upgrade; null leaking into an expected non-null slot.","solutions":["Verify the object's actual runtime type matches T before the call (use `is T`).","Fix the code that stored the value so it stores the expected type.","Null-check the value before casting.","Check for version mismatches between packages that share the object contract."],"exampleFix":"// before\nvar item = Guard.ThrowIfNotOfType<MyItem>(obj); // obj is actually OtherItem\n// after\nif (obj is MyItem item) { /* use item */ } else { /* handle mismatch */ }","handlingStrategy":"type-guard","validationCode":"if (value is not T) throw new InvalidCastException($\"Expected {typeof(T)}, got {value?.GetType().Name ?? \"null\"}\");","typeGuard":"if (value is T typed) { /* use typed */ } else { /* handle mismatch */ }","tryCatchPattern":"catch (InvalidCastException ex) { log.LogError(ex, \"payload type mismatch\"); }","preventionTips":["Use `is T` pattern matching instead of unchecked casts.","Keep the storage key/type contract in one shared constant/type.","Null-check before cast."],"tags":["casting","type-mismatch","guard"],"backgroundTag":"type-mismatch","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T09:17:21.228Z"}