< Summary

Information
Class: MyNet.UI.Navigation.Models.NavigationParameterConversions
Assembly: MyNet.UI
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/Navigation/Models/NavigationParameterConversions.cs
Tag: 323_28699572109
Line coverage
68%
Covered lines: 20
Uncovered lines: 9
Coverable lines: 29
Total lines: 77
Line coverage: 68.9%
Branch coverage
81%
Covered branches: 13
Total branches: 16
Branch coverage: 81.2%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
TryConvert(...)78.57%241462.5%
ConvertToEnum(...)100%22100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/Navigation/Models/NavigationParameterConversions.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="NavigationParameterConversions.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Globalization;
 9
 10namespace MyNet.UI.Navigation.Models;
 11
 12/// <summary>
 13/// Provides utility methods for converting raw navigation parameter values to strongly typed values, supporting various
 14/// </summary>
 15internal static class NavigationParameterConversions
 16{
 17    /// <summary>
 18    /// Attempts to convert a raw navigation parameter value to a strongly typed value of type T. Supports direct assign
 19    /// </summary>
 20    /// <param name="rawValue">The raw navigation parameter value.</param>
 21    /// <param name="value">The strongly typed value if the conversion is successful; otherwise, the default value of ty
 22    /// <typeparam name="T">The target type to convert to.</typeparam>
 23    /// <returns>True if the conversion is successful; otherwise, false.</returns>
 24    public static bool TryConvert<T>(object? rawValue, out T? value)
 25    {
 26        switch (rawValue)
 27        {
 28            case T typed:
 2129                value = typed;
 2130                return true;
 31            case null:
 032                value = default;
 033                return true;
 34        }
 35
 1536        var targetType = typeof(T);
 1537        var nullableUnderlying = Nullable.GetUnderlyingType(targetType);
 1538        if (nullableUnderlying is not null)
 039            targetType = nullableUnderlying;
 40
 41        try
 42        {
 43            var converted = targetType switch
 44            {
 2145                _ when targetType.IsEnum => ConvertToEnum(rawValue, targetType),
 1246                _ when targetType == typeof(Guid) && rawValue is string guidText => Guid.Parse(guidText),
 647                _ when rawValue is string text => Convert.ChangeType(text, targetType, CultureInfo.InvariantCulture),
 648                _ => Convert.ChangeType(rawValue, targetType, CultureInfo.InvariantCulture)
 49            };
 50
 1251            value = (T)converted;
 1252            return true;
 53        }
 054        catch (FormatException)
 55        {
 056            value = default;
 057            return false;
 58        }
 359        catch (InvalidCastException)
 60        {
 361            value = default;
 362            return false;
 63        }
 064        catch (OverflowException)
 65        {
 066            value = default;
 067            return false;
 68        }
 1569    }
 70
 671    private static object ConvertToEnum(object rawValue, Type enumType) => rawValue switch
 672    {
 373        string text => Enum.Parse(enumType, text, ignoreCase: true),
 374        _ => Enum.ToObject(enumType, Convert.ChangeType(rawValue, Enum.GetUnderlyingType(enumType), CultureInfo.Invarian
 675    };
 76}
 77