| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="RegistryValueConverter.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using System.Globalization; |
| | | 10 | | using System.Runtime.Versioning; |
| | | 11 | | using Microsoft.Win32; |
| | | 12 | | using MyNet.IO.Registry; |
| | | 13 | | |
| | | 14 | | namespace MyNet.Platform.Windows.Registry; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Provides functionality to convert .NET types to and from their corresponding registry value representations. This cl |
| | | 18 | | /// </summary> |
| | | 19 | | [SupportedOSPlatform("windows")] |
| | | 20 | | public sealed class RegistryValueConverter : IRegistryValueConverter |
| | | 21 | | { |
| | 3 | 22 | | private static readonly Dictionary<Type, RegistryValueKind> TypeMappings = new() |
| | 3 | 23 | | { |
| | 3 | 24 | | [typeof(string)] = RegistryValueKind.String, |
| | 3 | 25 | | [typeof(short)] = RegistryValueKind.DWord, |
| | 3 | 26 | | [typeof(ushort)] = RegistryValueKind.DWord, |
| | 3 | 27 | | [typeof(int)] = RegistryValueKind.DWord, |
| | 3 | 28 | | [typeof(uint)] = RegistryValueKind.DWord, |
| | 3 | 29 | | [typeof(long)] = RegistryValueKind.QWord, |
| | 3 | 30 | | [typeof(ulong)] = RegistryValueKind.QWord, |
| | 3 | 31 | | [typeof(bool)] = RegistryValueKind.DWord, |
| | 3 | 32 | | [typeof(byte[])] = RegistryValueKind.Binary, |
| | 3 | 33 | | [typeof(string[])] = RegistryValueKind.MultiString |
| | 3 | 34 | | }; |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Gets the corresponding <see cref="RegistryValueKind"/> for a given .NET type. This method checks if the provided |
| | | 38 | | /// </summary> |
| | | 39 | | /// <param name="type">The .NET type for which to get the corresponding registry value kind.</param> |
| | | 40 | | /// <returns>The <see cref="RegistryValueKind"/> that corresponds to the specified .NET type.</returns> |
| | | 41 | | public RegistryValueKind GetKind(Type type) |
| | | 42 | | { |
| | 24 | 43 | | type = Nullable.GetUnderlyingType(type) ?? type; |
| | | 44 | | |
| | 24 | 45 | | return type.IsEnum ? RegistryValueKind.String : TypeMappings.GetValueOrDefault(type, RegistryValueKind.String); |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Converts a .NET value to its corresponding registry value representation. This method takes an object as input a |
| | | 50 | | /// </summary> |
| | | 51 | | /// <param name="value">The .NET value to convert to a registry value representation.</param> |
| | | 52 | | /// <returns>The registry value representation of the specified .NET value.</returns> |
| | | 53 | | /// <exception cref="ArgumentNullException">Thrown if the provided value is null.</exception> |
| | | 54 | | public object ConvertTo(object value) |
| | | 55 | | { |
| | 66 | 56 | | ArgumentNullException.ThrowIfNull(value); |
| | | 57 | | |
| | 63 | 58 | | var type = value.GetType(); |
| | | 59 | | |
| | 63 | 60 | | type = Nullable.GetUnderlyingType(type) ?? type; |
| | | 61 | | |
| | 63 | 62 | | return type.IsEnum |
| | 63 | 63 | | ? value.ToString()! |
| | 63 | 64 | | : type == typeof(bool) |
| | 63 | 65 | | ? (bool)value ? 1 : 0 |
| | 63 | 66 | | : type == typeof(DateTime) |
| | 63 | 67 | | ? ((DateTime)value).ToString("O", CultureInfo.InvariantCulture) |
| | 63 | 68 | | : type == typeof(DateTimeOffset) |
| | 63 | 69 | | ? ((DateTimeOffset)value).ToString("O", CultureInfo.InvariantCulture) |
| | 63 | 70 | | : type == typeof(Guid) ? value.ToString()! : value; |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | /// <summary> |
| | | 74 | | /// Converts a registry value representation back to its corresponding .NET type. This method takes an object repres |
| | | 75 | | /// </summary> |
| | | 76 | | /// <param name="value">The registry value representation to convert.</param> |
| | | 77 | | /// <param name="targetType">The target .NET type to convert to.</param> |
| | | 78 | | /// <returns>The .NET value corresponding to the specified registry value representation.</returns> |
| | | 79 | | /// <exception cref="ArgumentNullException">Thrown if the provided value is null.</exception> |
| | | 80 | | public object ConvertFrom(object value, Type targetType) |
| | | 81 | | { |
| | 60 | 82 | | ArgumentNullException.ThrowIfNull(value); |
| | | 83 | | |
| | 57 | 84 | | var underlyingType = Nullable.GetUnderlyingType(targetType); |
| | 57 | 85 | | var effectiveType = underlyingType ?? targetType; |
| | | 86 | | |
| | 57 | 87 | | return effectiveType.IsEnum |
| | 57 | 88 | | ? Enum.Parse(effectiveType, value.ToString()!, true) |
| | 57 | 89 | | : effectiveType == typeof(bool) |
| | 57 | 90 | | ? Convert.ToBoolean(value, CultureInfo.InvariantCulture) |
| | 57 | 91 | | : effectiveType == typeof(DateTime) |
| | 57 | 92 | | ? DateTime.Parse( |
| | 57 | 93 | | value.ToString()!, |
| | 57 | 94 | | CultureInfo.InvariantCulture, |
| | 57 | 95 | | DateTimeStyles.RoundtripKind) |
| | 57 | 96 | | : effectiveType == typeof(DateTimeOffset) |
| | 57 | 97 | | ? DateTimeOffset.Parse( |
| | 57 | 98 | | value.ToString()!, |
| | 57 | 99 | | CultureInfo.InvariantCulture, |
| | 57 | 100 | | DateTimeStyles.RoundtripKind) |
| | 57 | 101 | | : effectiveType == typeof(Guid) |
| | 57 | 102 | | ? Guid.Parse(value.ToString()!) |
| | 57 | 103 | | : Convert.ChangeType( |
| | 57 | 104 | | value, |
| | 57 | 105 | | effectiveType, |
| | 57 | 106 | | CultureInfo.InvariantCulture); |
| | | 107 | | } |
| | | 108 | | } |
| | | 109 | | |