| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ConversionExtensions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Runtime.CompilerServices; |
| | | 9 | | using MyNet.Primitives.Conversion; |
| | | 10 | | |
| | | 11 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 12 | | namespace MyNet.Primitives; |
| | | 13 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 14 | | |
| | | 15 | | public static class ConversionExtensions |
| | | 16 | | { |
| | | 17 | | extension(double value) |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// Converts the value from one unit to another using the registered unit converters. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <param name="from">The unit of the input value.</param> |
| | | 23 | | /// <param name="to">The unit to convert the value to.</param> |
| | | 24 | | /// <typeparam name="TUnit">The type of the unit.</typeparam> |
| | | 25 | | /// <returns>The converted value.</returns> |
| | | 26 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 27 | | public double To<TUnit>(TUnit from, TUnit to) |
| | 99 | 28 | | where TUnit : struct, Enum => UnitConverterRegistry.Get<TUnit>().Convert(value, from, to); |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Converts the value from its current unit to another unit using the registered unit converters. The current u |
| | | 32 | | /// </summary> |
| | | 33 | | /// <param name="to">The unit to convert the value to.</param> |
| | | 34 | | /// <typeparam name="TUnit">The type of the unit.</typeparam> |
| | | 35 | | /// <returns>The converted value.</returns> |
| | | 36 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 37 | | public double To<TUnit>(TUnit to) |
| | 0 | 38 | | where TUnit : struct, Enum => value.To(default, to); |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Converts the value from one unit to another using the registered unit converters. The type of the unit is de |
| | | 42 | | /// </summary> |
| | | 43 | | /// <param name="type">The type of the unit.</param> |
| | | 44 | | /// <param name="from">The unit of the input value.</param> |
| | | 45 | | /// <param name="to">The unit to convert the value to.</param> |
| | | 46 | | /// <returns>The converted value.</returns> |
| | | 47 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 48 | | public double To<TUnit>(Type type, TUnit from, TUnit to) |
| | 0 | 49 | | where TUnit : struct, Enum => UnitConverterRegistry.Get<TUnit>(type).Convert(value, from, to); |
| | | 50 | | } |
| | | 51 | | } |
| | | 52 | | |