| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="NavigationParameterConversions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Globalization; |
| | | 9 | | |
| | | 10 | | namespace 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> |
| | | 15 | | internal 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: |
| | 21 | 29 | | value = typed; |
| | 21 | 30 | | return true; |
| | | 31 | | case null: |
| | 0 | 32 | | value = default; |
| | 0 | 33 | | return true; |
| | | 34 | | } |
| | | 35 | | |
| | 15 | 36 | | var targetType = typeof(T); |
| | 15 | 37 | | var nullableUnderlying = Nullable.GetUnderlyingType(targetType); |
| | 15 | 38 | | if (nullableUnderlying is not null) |
| | 0 | 39 | | targetType = nullableUnderlying; |
| | | 40 | | |
| | | 41 | | try |
| | | 42 | | { |
| | | 43 | | var converted = targetType switch |
| | | 44 | | { |
| | 21 | 45 | | _ when targetType.IsEnum => ConvertToEnum(rawValue, targetType), |
| | 12 | 46 | | _ when targetType == typeof(Guid) && rawValue is string guidText => Guid.Parse(guidText), |
| | 6 | 47 | | _ when rawValue is string text => Convert.ChangeType(text, targetType, CultureInfo.InvariantCulture), |
| | 6 | 48 | | _ => Convert.ChangeType(rawValue, targetType, CultureInfo.InvariantCulture) |
| | | 49 | | }; |
| | | 50 | | |
| | 12 | 51 | | value = (T)converted; |
| | 12 | 52 | | return true; |
| | | 53 | | } |
| | 0 | 54 | | catch (FormatException) |
| | | 55 | | { |
| | 0 | 56 | | value = default; |
| | 0 | 57 | | return false; |
| | | 58 | | } |
| | 3 | 59 | | catch (InvalidCastException) |
| | | 60 | | { |
| | 3 | 61 | | value = default; |
| | 3 | 62 | | return false; |
| | | 63 | | } |
| | 0 | 64 | | catch (OverflowException) |
| | | 65 | | { |
| | 0 | 66 | | value = default; |
| | 0 | 67 | | return false; |
| | | 68 | | } |
| | 15 | 69 | | } |
| | | 70 | | |
| | 6 | 71 | | private static object ConvertToEnum(object rawValue, Type enumType) => rawValue switch |
| | 6 | 72 | | { |
| | 3 | 73 | | string text => Enum.Parse(enumType, text, ignoreCase: true), |
| | 3 | 74 | | _ => Enum.ToObject(enumType, Convert.ChangeType(rawValue, Enum.GetUnderlyingType(enumType), CultureInfo.Invarian |
| | 6 | 75 | | }; |
| | | 76 | | } |
| | | 77 | | |