1
1
using System ;
2
2
using System . Collections . Generic ;
3
+ using System . Globalization ;
3
4
using System . Linq ;
5
+ using JetBrains . Annotations ;
4
6
using UnitsNet . InternalHelpers ;
5
7
6
8
namespace UnitsNet
@@ -15,17 +17,9 @@ static Quantity()
15
17
Types = quantityTypes ;
16
18
Names = quantityTypes . Select ( qt => qt . ToString ( ) ) . ToArray ( ) ;
17
19
18
- // A bunch of reflection to enumerate quantity types, instantiate with the default constructor and return its QuantityInfo property
19
- InfosLazy = new Lazy < QuantityInfo [ ] > ( ( ) => typeof ( Length )
20
- . Wrap ( )
21
- . Assembly
22
- . GetExportedTypes ( )
23
- . Where ( typeof ( IQuantity ) . IsAssignableFrom )
24
- . Where ( t => t . Wrap ( ) . IsClass || t . Wrap ( ) . IsValueType ) // Future-proofing: Considering changing quantities from struct to class
25
- . Select ( Activator . CreateInstance )
26
- . Cast < IQuantity > ( )
27
- . Select ( q => q . QuantityInfo )
28
- . OrderBy ( q => q . Name )
20
+ InfosLazy = new Lazy < QuantityInfo [ ] > ( ( ) => Types
21
+ . Select ( quantityType => FromQuantityType ( quantityType , 0.0 ) . QuantityInfo )
22
+ . OrderBy ( quantityInfo => quantityInfo . Name )
29
23
. ToArray ( ) ) ;
30
24
}
31
25
@@ -60,6 +54,55 @@ public static IQuantity From(QuantityValue value, Enum unit)
60
54
$ "Unit value { unit } of type { unit . GetType ( ) } is not a known unit enum type. Expected types like UnitsNet.Units.LengthUnit. Did you pass in a third-party enum type defined outside UnitsNet library?") ;
61
55
}
62
56
57
+ /// <inheritdoc cref="TryFrom(QuantityValue,System.Enum,out UnitsNet.IQuantity)"/>
58
+ public static bool TryFrom ( double value , Enum unit , out IQuantity quantity )
59
+ {
60
+ // Implicit cast to QuantityValue would prevent TryFrom from being called,
61
+ // so we need to explicitly check this here for double arguments.
62
+ if ( double . IsNaN ( value ) || double . IsInfinity ( value ) )
63
+ {
64
+ quantity = default ( IQuantity ) ;
65
+ return false ;
66
+ }
67
+
68
+ return TryFrom ( ( QuantityValue ) value , unit , out quantity ) ;
69
+ }
70
+
71
+ /// <inheritdoc cref="Parse(IFormatProvider, System.Type,string)"/>
72
+ public static IQuantity Parse ( Type quantityType , string quantityString ) => Parse ( null , quantityType , quantityString ) ;
73
+
74
+ /// <summary>
75
+ /// Dynamically parse a quantity string representation.
76
+ /// </summary>
77
+ /// <param name="formatProvider">The format provider to use for lookup. Defaults to <see cref="CultureInfo.CurrentUICulture" /> if null.</param>
78
+ /// <param name="quantityType">Type of quantity, such as <see cref="Length"/>.</param>
79
+ /// <param name="quantityString">Quantity string representation, such as "1.5 kg". Must be compatible with given quantity type.</param>
80
+ /// <returns>The parsed quantity.</returns>
81
+ /// <exception cref="ArgumentException">Type must be of type UnitsNet.IQuantity -or- Type is not a known quantity type.</exception>
82
+ public static IQuantity Parse ( [ CanBeNull ] IFormatProvider formatProvider , Type quantityType , string quantityString )
83
+ {
84
+ if ( ! typeof ( IQuantity ) . Wrap ( ) . IsAssignableFrom ( quantityType ) )
85
+ throw new ArgumentException ( $ "Type { quantityType } must be of type UnitsNet.IQuantity.") ;
86
+
87
+ if ( TryParse ( formatProvider , quantityType , quantityString , out IQuantity quantity ) ) return quantity ;
88
+
89
+ throw new ArgumentException ( $ "Quantity string could not be parsed to quantity { quantityType } .") ;
90
+ }
91
+
92
+ /// <inheritdoc cref="TryParse(IFormatProvider,System.Type,string,out UnitsNet.IQuantity)"/>
93
+ public static bool TryParse ( Type quantityType , string quantityString , out IQuantity quantity ) =>
94
+ TryParse ( null , quantityType , quantityString , out quantity ) ;
95
+
96
+ /// <summary>
97
+ /// Get information about the given quantity type.
98
+ /// </summary>
99
+ /// <param name="quantityType">The quantity type enum value.</param>
100
+ /// <returns>Information about the quantity and its units.</returns>
101
+ public static QuantityInfo GetInfo ( QuantityType quantityType )
102
+ {
103
+ return Infos . First ( qi => qi . QuantityType == quantityType ) ;
104
+ }
105
+
63
106
/// <summary>
64
107
/// Get a list of quantities that has the given base dimensions.
65
108
/// </summary>
0 commit comments