{"record":{"id":"ed2c02ff0a5a2033","repo":"PrismLibrary/Prism","slug":"unable-to-convert-the-value-of-type-kvp-value-gettype","errorCode":null,"errorMessage":"Unable to convert the value of Type '{kvp.Value.GetType().FullName}' to '{type.FullName}' for the key '{key}' ","messagePattern":"Unable to convert the value of Type '(.+?)' to '(.+?)' for the key '(.+?)' ","errorType":"exception","errorClass":"InvalidCastException","httpStatus":null,"severity":"error","filePath":"src/Prism.Core/Common/ParametersExtensions.cs","lineNumber":45,"sourceCode":"        /// <summary>\n        /// Searches <paramref name=\"parameters\"/> for value referenced by <paramref name=\"key\"/>\n        /// </summary>\n        /// <param name=\"parameters\">A collection of parameters to search</param>\n        /// <param name=\"key\">The key of the parameter to find</param>\n        /// <param name=\"type\">The type of the parameter to return</param>\n        /// <returns>A matching value of <paramref name=\"type\"/> if it exists</returns>\n        /// <exception cref=\"InvalidCastException\">Unable to convert the value of Type</exception>\n        [EditorBrowsable(EditorBrowsableState.Never)]\n        public static object GetValue(this IEnumerable<KeyValuePair<string, object>> parameters, string key, Type type)\n        {\n            foreach (var kvp in parameters)\n            {\n                if (string.Compare(kvp.Key, key, StringComparison.Ordinal) == 0)\n                {\n                    if (TryGetValueInternal(kvp, type, out var value))\n                        return value;\n\n                    throw new InvalidCastException($\"Unable to convert the value of Type '{kvp.Value.GetType().FullName}' to '{type.FullName}' for the key '{key}' \");\n                }\n            }\n\n            return GetDefault(type);\n        }\n\n        /// <summary>\n        /// Searches <paramref name=\"parameters\"/> for value referenced by <paramref name=\"key\"/>\n        /// </summary>\n        /// <typeparam name=\"T\">The type of the parameter to return</typeparam>\n        /// <param name=\"parameters\">A collection of parameters to search</param>\n        /// <param name=\"key\">The key of the parameter to find</param>\n        /// <param name=\"value\">The value of parameter to return</param>\n        /// <returns>Success if value is found; otherwise returns <c>false</c></returns>\n        [EditorBrowsable(EditorBrowsableState.Never)]\n        public static bool TryGetValue<T>(this IEnumerable<KeyValuePair<string, object>> parameters, string key, out T value)\n        {\n            var type = typeof(T);","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Prism.Core/Common/ParametersExtensions.cs#L27-L63","documentation":"ParametersExtensions.GetValue iterates the navigation parameters and casts the stored value to the requested type T. When a parameter with the matching key exists but its runtime type cannot be converted to T, it throws InvalidCastException naming both types and the key.","triggerScenarios":"Navigating with NavigationParameters and calling GetValue<string>(\"id\") on a parameter stored as int (or vice versa), or GetValue on a custom type stored under a different concrete type.","commonSituations":"Prism INavigationAware/OnNavigatedTo code where the sending page stores an int id but the receiving page reads it as string; type mismatches after refactorings.","solutions":["Align the type argument with the stored value's type (GetValue<int>(\"id\"))","Convert explicitly: value?.ToString() before adding, or int.Parse after reading","Use GetValue<object> and Convert.ChangeType for tolerant conversion"],"exampleFix":"// before\nvar id = parameters.GetValue<string>(\"id\"); // stored as int\n// after\nvar id = parameters.GetValue<int>(\"id\").ToString();","handlingStrategy":"type-guard","validationCode":"object raw = parameters.TryGetValue(key, out var v) ? v : null;\nif (raw is not T typed) { /* convert or fail fast */ }","typeGuard":"bool TryGetTyped<T>(INavigationParameters p, string key, out T value) where T : IConvertible {\n    value = default;\n    if (!p.TryGetValue<object>(key, out var raw) || raw is not T t) return false;\n    value = t; return true;\n}","tryCatchPattern":"try { var id = parameters.GetValue<T>(key); }\ncatch (InvalidCastException ex) { logger.LogError(ex, \"Parameter '{Key}' has unexpected type\", key); }","preventionTips":["Use a shared constants class for parameter keys and their types","Convert values to a canonical type when adding to NavigationParameters","Write tests asserting parameter types between sending/receiving pages"],"tags":["invalid-cast","navigation","prism","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"358118cd640d9a22ff8cf21c8ad197fa038b7990","analyzedAt":"2026-09-15T15:00:31.079Z","contentChangedAt":"2026-09-15T15:00:31.079Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}