You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
QuantityInfo: internalizing the UnitInfo construction (#1555)
- `QuantityInfo`: internalizing the `UnitInfo` construction
- `QuantityInfo`: introducing a delegate for constructing the quantity
(required only for net standard)
- `QuantityInfo`: introducing an optional `ResourceDictionary`
- `QuantityInfo`: replacing the `TUnit[]` with an
`IReadOnlyCollection<TUnit>`
- `UnitInfo`: introducing a back-reference to the `QuantityInfo` (making
the `QuantityName` `[Obsolete]`)
- `IQuantity`: added the `QuantityInfo<TQuantity, TUnit>`, the
`From(double, TUnit)` method and default implementations for the
non-generic properties
- `QuantityInfoLookup`: added another collection for the quantity by
type mapping (replacing the generated code in `Quantity.g.s`).
- `UnitAbbreviationsCache`: `ReadAbbreviationsFromResourceFile`
implemented using the provided `ResourceManager` (if available)
- updating the `QuantityInfo` definitions for all quantities
(introducing a concrete class, such as `MassInfo`) with helpers for
creating a derived configuration
- `HowMuch` upgraded to `IQuantity<HowMuch, HowMuchUnit>` (the original
`QuantityInfo` is now abstract)
- `Quantity` refactored the `Parse` / `From*` methods using the default
`QuantityParser` / `QuantityInfoLookup`
- `Quantity` replaced the `ByName` dictionary with an
`IReadOnlyDictionary`
- `UnitsNet.csproj` / `UnitsNet.Tests.csproj`: added some (specific)
implicit usings
---------
Co-authored-by: Andreas Gullberg Larsen <[email protected]>
/// Provides detailed information about the <see cref=""{_quantity.Name}""/> quantity, including its name, base unit, unit mappings, base dimensions, and conversion functions.
155
+
/// </summary>
156
+
public sealed class {quantityInfoClassName}: QuantityInfo<{_quantity.Name}, {_unitEnumName}>
: this(name, baseUnit, unitMappings, zero, baseDimensions, {_quantity.Name}.From, new ResourceManager(""UnitsNet.GeneratedCode.Resources.{_quantity.Name}"", typeof({_quantity.Name}).Assembly))
169
+
{{
170
+
}}
171
+
172
+
/// <summary>
173
+
/// Creates a new instance of the <see cref=""{quantityInfoClassName}""/> class with the default settings for the {_quantity.Name} quantity.
174
+
/// </summary>
175
+
/// <returns>A new instance of the <see cref=""{quantityInfoClassName}""/> class with the default settings.</returns>
176
+
public static {quantityInfoClassName} CreateDefault()
177
+
{{
178
+
return new {quantityInfoClassName}(nameof({_quantity.Name}), DefaultBaseUnit, GetDefaultMappings(), new {_quantity.Name}(0, DefaultBaseUnit), DefaultBaseDimensions);
179
+
}}
180
+
181
+
/// <summary>
182
+
/// Creates a new instance of the <see cref=""{quantityInfoClassName}""/> class with the default settings for the {_quantity.Name} quantity and a callback for customizing the default unit mappings.
183
+
/// </summary>
184
+
/// <param name=""customizeUnits"">
185
+
/// A callback function for customizing the default unit mappings.
186
+
/// </param>
187
+
/// <returns>
188
+
/// A new instance of the <see cref=""{quantityInfoClassName}""/> class with the default settings.
189
+
/// </returns>
190
+
public static {quantityInfoClassName} CreateDefault(Func<IEnumerable<UnitDefinition<{_unitEnumName}>>, IEnumerable<IUnitDefinition<{_unitEnumName}>>> customizeUnits)
191
+
{{
192
+
return new {quantityInfoClassName}(nameof({_quantity.Name}), DefaultBaseUnit, customizeUnits(GetDefaultMappings()), new {_quantity.Name}(0, DefaultBaseUnit), DefaultBaseDimensions);
193
+
}}
162
194
195
+
/// <summary>
196
+
/// The <see cref=""BaseDimensions"" /> for <see cref=""{_quantity.Name}""/> is {_quantity.BaseDimensions}.
197
+
/// </summary>
198
+
public static BaseDimensions DefaultBaseDimensions {{ get; }} = {createDimensionsExpression};
199
+
200
+
/// <summary>
201
+
/// The default base unit of {_quantity.Name} is {_baseUnit.SingularName}. All conversions, as defined in the <see cref=""GetDefaultMappings""/>, go via this value.
202
+
/// </summary>
203
+
public static {_unitEnumName} DefaultBaseUnit {{ get; }} = {_unitEnumName}.{_baseUnit.SingularName};
204
+
205
+
/// <summary>
206
+
/// Retrieves the default mappings for <see cref=""{_unitEnumName}""/>.
207
+
/// </summary>
208
+
/// <returns>An <see cref=""IEnumerable{{T}}""/> of <see cref=""UnitDefinition{{{_unitEnumName}}}""/> representing the default unit mappings for {_quantity.Name}.</returns>
209
+
public static IEnumerable<UnitDefinition<{_unitEnumName}>> GetDefaultMappings()
210
+
{{");
211
+
163
212
foreach(Unitunitin_quantity.Units)
164
213
{
165
214
BaseUnits?baseUnits=unit.BaseUnits;
215
+
stringbaseUnitsFormat;
166
216
if(baseUnits==null)
167
217
{
168
-
Writer.WL($@"
169
-
new UnitInfo<{_unitEnumName}>({_unitEnumName}.{unit.SingularName}, ""{unit.PluralName}"", BaseUnits.Undefined, ""{_quantity.Name}""),");
new UnitInfo<{_unitEnumName}>({_unitEnumName}.{unit.SingularName}, ""{unit.PluralName}"", new BaseUnits({baseUnitsCtorArgs}), ""{_quantity.Name}""),");
232
+
}.Where(str =>str!=null))})";
187
233
}
234
+
235
+
Writer.WL($@"
236
+
yield return new ({_unitEnumName}.{unit.SingularName}, ""{unit.SingularName}"", ""{unit.PluralName}"", {baseUnitsFormat});");
/// Dynamically parse or construct quantities when types are only known at runtime.
25
+
/// </summary>
26
+
public partial class Quantity
29
27
{
30
28
/// <summary>
31
-
/// Dynamically parse or construct quantities when types are only known at runtime.
29
+
/// Serves as a repository for predefined quantity conversion mappings, facilitating the automatic generation and retrieval of unit conversions in the UnitsNet library.
32
30
/// </summary>
33
-
public partial class Quantity
31
+
internal static class Provider
34
32
{
35
33
/// <summary>
36
-
/// All QuantityInfo instances mapped by quantity name that are present in UnitsNet by default.
34
+
/// All QuantityInfo instances that are present in UnitsNet by default.
37
35
/// </summary>
38
-
public static readonly IDictionary<string, QuantityInfo> ByName = new Dictionary<string, QuantityInfo>
36
+
internal static IReadOnlyList<QuantityInfo> DefaultQuantities => new QuantityInfo[]
/// Try to dynamically parse a quantity string representation.
97
-
/// </summary>
98
-
/// <param name=""formatProvider"">The format provider to use for lookup. Defaults to <see cref=""CultureInfo.CurrentCulture"" /> if null.</param>
99
-
/// <param name=""quantityType"">Type of quantity, such as <see cref=""Length""/>.</param>
100
-
/// <param name=""quantityString"">Quantity string representation, such as ""1.5 kg"". Must be compatible with given quantity type.</param>
101
-
/// <param name=""quantity"">The resulting quantity if successful, otherwise <c>default</c>.</param>
102
-
/// <returns>The parsed quantity.</returns>
103
-
public static bool TryParse(IFormatProvider? formatProvider, Type quantityType, [NotNullWhen(true)] string? quantityString, [NotNullWhen(true)] out IQuantity? quantity)
104
-
{
105
-
quantity = default(IQuantity);
106
-
107
-
if (!typeof(IQuantity).IsAssignableFrom(quantityType))
108
-
return false;
109
-
110
-
var parser = UnitsNetSetup.Default.QuantityParser;
111
-
112
-
return quantityType switch
113
-
{");
114
-
foreach(varquantityin_quantities)
115
-
{
116
-
varquantityName=quantity.Name;
117
-
Writer.WL($@"
118
-
Type _ when quantityType == typeof({quantityName}) => parser.TryParse<{quantityName}, {quantityName}Unit>(quantityString, formatProvider, {quantityName}.From, out quantity),");
0 commit comments