< Summary

Information
Class: MyNet.Platform.Windows.Registry.RegistryValueConverter
Assembly: MyNet.Platform.Windows
File(s): https://raw.githubusercontent.com/sandre58/MyNet/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.Platform.Windows/Registry/RegistryValueConverter.cs
Tag: 342_31701152804
Line coverage
100%
Covered lines: 50
Uncovered lines: 0
Coverable lines: 50
Total lines: 109
Line coverage: 100%
Branch coverage
92%
Covered branches: 26
Total branches: 28
Branch coverage: 92.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.cctor()100%11100%
GetKind(...)100%44100%
ConvertTo(...)91.66%1212100%
ConvertFrom(...)91.66%1212100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/5aaaab311aa14541478b9e402357d98b657a23e4/src/MyNet.Platform.Windows/Registry/RegistryValueConverter.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="RegistryValueConverter.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using System.Collections.Generic;
 9using System.Globalization;
 10using System.Runtime.Versioning;
 11using Microsoft.Win32;
 12using MyNet.IO.Registry;
 13
 14namespace 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")]
 20public sealed class RegistryValueConverter : IRegistryValueConverter
 21{
 322    private static readonly Dictionary<Type, RegistryValueKind> TypeMappings = new()
 323    {
 324        [typeof(string)] = RegistryValueKind.String,
 325        [typeof(short)] = RegistryValueKind.DWord,
 326        [typeof(ushort)] = RegistryValueKind.DWord,
 327        [typeof(int)] = RegistryValueKind.DWord,
 328        [typeof(uint)] = RegistryValueKind.DWord,
 329        [typeof(long)] = RegistryValueKind.QWord,
 330        [typeof(ulong)] = RegistryValueKind.QWord,
 331        [typeof(bool)] = RegistryValueKind.DWord,
 332        [typeof(byte[])] = RegistryValueKind.Binary,
 333        [typeof(string[])] = RegistryValueKind.MultiString
 334    };
 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    {
 2443        type = Nullable.GetUnderlyingType(type) ?? type;
 44
 2445        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    {
 6656        ArgumentNullException.ThrowIfNull(value);
 57
 6358        var type = value.GetType();
 59
 6360        type = Nullable.GetUnderlyingType(type) ?? type;
 61
 6362        return type.IsEnum
 6363            ? value.ToString()!
 6364            : type == typeof(bool)
 6365            ? (bool)value ? 1 : 0
 6366            : type == typeof(DateTime)
 6367            ? ((DateTime)value).ToString("O", CultureInfo.InvariantCulture)
 6368            : type == typeof(DateTimeOffset)
 6369            ? ((DateTimeOffset)value).ToString("O", CultureInfo.InvariantCulture)
 6370            : 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    {
 6082        ArgumentNullException.ThrowIfNull(value);
 83
 5784        var underlyingType = Nullable.GetUnderlyingType(targetType);
 5785        var effectiveType = underlyingType ?? targetType;
 86
 5787        return effectiveType.IsEnum
 5788            ? Enum.Parse(effectiveType, value.ToString()!, true)
 5789            : effectiveType == typeof(bool)
 5790            ? Convert.ToBoolean(value, CultureInfo.InvariantCulture)
 5791            : effectiveType == typeof(DateTime)
 5792            ? DateTime.Parse(
 5793                value.ToString()!,
 5794                CultureInfo.InvariantCulture,
 5795                DateTimeStyles.RoundtripKind)
 5796            : effectiveType == typeof(DateTimeOffset)
 5797            ? DateTimeOffset.Parse(
 5798                value.ToString()!,
 5799                CultureInfo.InvariantCulture,
 57100                DateTimeStyles.RoundtripKind)
 57101            : effectiveType == typeof(Guid)
 57102            ? Guid.Parse(value.ToString()!)
 57103            : Convert.ChangeType(
 57104            value,
 57105            effectiveType,
 57106            CultureInfo.InvariantCulture);
 107    }
 108}
 109