PrismLibrary/Prism · error · InvalidCastException

Unable to convert the value of Type

Error message

Unable to convert the value of Type '{kvp.Value.GetType().FullName}' to '{type.FullName}' for the key '{key}' 

What it means

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.

Solutions

  1. Align the type argument with the stored value's type (GetValue<int>("id"))
  2. Convert explicitly: value?.ToString() before adding, or int.Parse after reading
  3. Use GetValue<object> and Convert.ChangeType for tolerant conversion

Example fix

// before
var id = parameters.GetValue<string>("id"); // stored as int
// after
var id = parameters.GetValue<int>("id").ToString();
Defensive patterns

Strategy: type-guard

Validate before calling

object raw = parameters.TryGetValue(key, out var v) ? v : null;
if (raw is not T typed) { /* convert or fail fast */ }

Type guard

bool TryGetTyped<T>(INavigationParameters p, string key, out T value) where T : IConvertible {
    value = default;
    if (!p.TryGetValue<object>(key, out var raw) || raw is not T t) return false;
    value = t; return true;
}

Try / catch

try { var id = parameters.GetValue<T>(key); }
catch (InvalidCastException ex) { logger.LogError(ex, "Parameter '{Key}' has unexpected type", key); }

Prevention

When it happens

Trigger: 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.

Common situations: Prism INavigationAware/OnNavigatedTo code where the sending page stores an int id but the receiving page reads it as string; type mismatches after refactorings.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of PrismLibrary/Prism@358118cd64 (2026-09-15). Data as JSON: /api/errors/ed2c02ff0a5a2033. Report an issue: GitHub.

Appendix: source

Thrown at src/Prism.Core/Common/ParametersExtensions.cs:45

        /// <summary>
        /// Searches <paramref name="parameters"/> for value referenced by <paramref name="key"/>
        /// </summary>
        /// <param name="parameters">A collection of parameters to search</param>
        /// <param name="key">The key of the parameter to find</param>
        /// <param name="type">The type of the parameter to return</param>
        /// <returns>A matching value of <paramref name="type"/> if it exists</returns>
        /// <exception cref="InvalidCastException">Unable to convert the value of Type</exception>
        [EditorBrowsable(EditorBrowsableState.Never)]
        public static object GetValue(this IEnumerable<KeyValuePair<string, object>> parameters, string key, Type type)
        {
            foreach (var kvp in parameters)
            {
                if (string.Compare(kvp.Key, key, StringComparison.Ordinal) == 0)
                {
                    if (TryGetValueInternal(kvp, type, out var value))
                        return value;

                    throw new InvalidCastException($"Unable to convert the value of Type '{kvp.Value.GetType().FullName}' to '{type.FullName}' for the key '{key}' ");
                }
            }

            return GetDefault(type);
        }

        /// <summary>
        /// Searches <paramref name="parameters"/> for value referenced by <paramref name="key"/>
        /// </summary>
        /// <typeparam name="T">The type of the parameter to return</typeparam>
        /// <param name="parameters">A collection of parameters to search</param>
        /// <param name="key">The key of the parameter to find</param>
        /// <param name="value">The value of parameter to return</param>
        /// <returns>Success if value is found; otherwise returns <c>false</c></returns>
        [EditorBrowsable(EditorBrowsableState.Never)]
        public static bool TryGetValue<T>(this IEnumerable<KeyValuePair<string, object>> parameters, string key, out T value)
        {
            var type = typeof(T);

View on GitHub (pinned to 358118cd64)