|
| 1 | +// Copyright (c) 2013 Andreas Gullberg Larsen ([email protected]). |
| 2 | +// https://github.com/angularsen/UnitsNet |
| 3 | +// |
| 4 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +// of this software and associated documentation files (the "Software"), to deal |
| 6 | +// in the Software without restriction, including without limitation the rights |
| 7 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +// copies of the Software, and to permit persons to whom the Software is |
| 9 | +// furnished to do so, subject to the following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included in |
| 12 | +// all copies or substantial portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | +// THE SOFTWARE. |
| 21 | + |
| 22 | +using System; |
| 23 | +using System.Collections.Generic; |
| 24 | +using System.Linq; |
| 25 | +using System.Reflection; |
| 26 | +using UnitsNet.Units; |
| 27 | + |
| 28 | +namespace UnitsNet |
| 29 | +{ |
| 30 | + /// <summary> |
| 31 | + /// This class helps enumerating quantities and units at runtime. |
| 32 | + /// </summary> |
| 33 | + public static class UnitsHelper |
| 34 | + { |
| 35 | + private static readonly string UnitEnumNamespace = typeof(LengthUnit).Namespace; |
| 36 | + |
| 37 | + private static readonly Type[] UnitEnumTypes = Assembly.GetAssembly(typeof(Length)) |
| 38 | + .GetExportedTypes() |
| 39 | + .Where(t => t.IsEnum && t.Namespace == UnitEnumNamespace) |
| 40 | + .ToArray(); |
| 41 | + |
| 42 | + static UnitsHelper() |
| 43 | + { |
| 44 | + var quantityTypes = Enum.GetValues(typeof(QuantityType)).Cast<QuantityType>().ToArray(); |
| 45 | + Quantities = quantityTypes; |
| 46 | + QuantityNames = quantityTypes.Select(q => q.ToString()).ToArray(); |
| 47 | + } |
| 48 | + |
| 49 | + /// <summary> |
| 50 | + /// All enum values of <see cref="QuantityType"/>, such as <see cref="QuantityType.Length"/> and <see cref="QuantityType.Mass"/>. |
| 51 | + /// </summary> |
| 52 | + public static QuantityType[] Quantities { get; } |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// All enum value names of <see cref="QuantityType"/>, such as "Length" and "Mass". |
| 56 | + /// </summary> |
| 57 | + public static string[] QuantityNames { get; } |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// Returns the enum values for the given <paramref name="quantity"/>, excluding the Undefined=0 value. |
| 61 | + /// You can then use this for dynamic parsing in <see cref="UnitParser.Parse"/>. |
| 62 | + /// If you only need the unit names, use <see cref="GetUnitNamesForQuantity"/> instead. |
| 63 | + /// </summary> |
| 64 | + /// <example> |
| 65 | + /// Given <see cref="QuantityType.Length"/> it will return all enum values of <see cref="LengthUnit"/>, |
| 66 | + /// such as <see cref="LengthUnit.Centimeter"/> and <see cref="LengthUnit.Meter"/>. |
| 67 | + /// </example> |
| 68 | + /// <param name="quantity">The quantity type.</param> |
| 69 | + /// <returns>Unit enum values.</returns> |
| 70 | + public static IEnumerable<object> GetUnitEnumValuesForQuantity(QuantityType quantity) |
| 71 | + { |
| 72 | + // QuantityType.Length => "UnitsNet.Units.LengthUnit" |
| 73 | + Type unitEnumType = GetUnitType(quantity); |
| 74 | + |
| 75 | + // Skip Undefined, which is only really used to help catch uninitialized values |
| 76 | + return Enum.GetValues(unitEnumType).Cast<object>().Skip(1).ToArray(); |
| 77 | + } |
| 78 | + |
| 79 | + /// <summary> |
| 80 | + /// Returns the enum value names for the given <paramref name="quantity"/>. |
| 81 | + /// For example, given <see cref="QuantityType.Length"/> it will return the names |
| 82 | + /// of all enum values of <see cref="LengthUnit"/>, such as "Centimeter" and "Meter". |
| 83 | + /// </summary> |
| 84 | + /// <param name="quantity">The quantity type.</param> |
| 85 | + /// <returns>Unit enum values.</returns> |
| 86 | + public static IEnumerable<string> GetUnitNamesForQuantity(QuantityType quantity) |
| 87 | + { |
| 88 | + return GetUnitEnumValuesForQuantity(quantity).Select(unitEnumValue => unitEnumValue.ToString()); |
| 89 | + } |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// Returns the enum type for the given <paramref name="quantity"/>. |
| 93 | + /// </summary> |
| 94 | + /// <example> |
| 95 | + /// Given <see cref="QuantityType.Length"/> it will return <see cref="LengthUnit"/>. |
| 96 | + /// </example> |
| 97 | + /// <param name="quantity"></param> |
| 98 | + /// <returns></returns> |
| 99 | + public static Type GetUnitType(QuantityType quantity) |
| 100 | + { |
| 101 | + return UnitEnumTypes |
| 102 | + .First(t => t.FullName == $"{UnitEnumNamespace}.{quantity}Unit"); |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments