Skip to content

Commit 52aba98

Browse files
committed
♻️Replace obsolete method usages with UnitsNetSetup.Default
Replace obsolete static methods for UnitAbbreviations, QuantityParser, UnitParser and UnitConverter, with the new `UnitsNetSetup.Default` static instance.
1 parent 89c3017 commit 52aba98

File tree

269 files changed

+949
-810
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

269 files changed

+949
-810
lines changed

CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ public static string GetAbbreviation({_unitEnumName} unit)
379379
/// <param name=""provider"">Format to use for localization. Defaults to <see cref=""CultureInfo.CurrentCulture"" /> if null.</param>
380380
public static string GetAbbreviation({_unitEnumName} unit, IFormatProvider? provider)
381381
{{
382-
return UnitAbbreviationsCache.Default.GetDefaultAbbreviation(unit, provider);
382+
return UnitsNetSetup.Default.UnitAbbreviations.GetDefaultAbbreviation(unit, provider);
383383
}}
384384
385385
#endregion
@@ -484,7 +484,7 @@ private void GenerateStaticParseMethods()
484484
/// <param name=""provider"">Format to use when parsing number and unit. Defaults to <see cref=""CultureInfo.CurrentCulture"" /> if null.</param>
485485
public static {_quantity.Name} Parse(string str, IFormatProvider? provider)
486486
{{
487-
return QuantityParser.Default.Parse<{_quantity.Name}, {_unitEnumName}>(
487+
return UnitsNetSetup.Default.QuantityParser.Parse<{_quantity.Name}, {_unitEnumName}>(
488488
str,
489489
provider,
490490
From);
@@ -515,7 +515,7 @@ public static bool TryParse(string? str, out {_quantity.Name} result)
515515
/// <param name=""provider"">Format to use when parsing number and unit. Defaults to <see cref=""CultureInfo.CurrentCulture"" /> if null.</param>
516516
public static bool TryParse(string? str, IFormatProvider? provider, out {_quantity.Name} result)
517517
{{
518-
return QuantityParser.Default.TryParse<{_quantity.Name}, {_unitEnumName}>(
518+
return UnitsNetSetup.Default.QuantityParser.TryParse<{_quantity.Name}, {_unitEnumName}>(
519519
str,
520520
provider,
521521
From,
@@ -548,7 +548,7 @@ public static bool TryParse(string? str, IFormatProvider? provider, out {_quanti
548548
/// <exception cref=""UnitsNetException"">Error parsing string.</exception>
549549
public static {_unitEnumName} ParseUnit(string str, IFormatProvider? provider)
550550
{{
551-
return UnitParser.Default.Parse<{_unitEnumName}>(str, provider);
551+
return UnitsNetSetup.Default.UnitParser.Parse<{_unitEnumName}>(str, provider);
552552
}}
553553
554554
/// <inheritdoc cref=""TryParseUnit(string,IFormatProvider,out UnitsNet.Units.{_unitEnumName})""/>
@@ -569,7 +569,7 @@ public static bool TryParseUnit(string str, out {_unitEnumName} unit)
569569
/// <param name=""provider"">Format to use when parsing number and unit. Defaults to <see cref=""CultureInfo.CurrentCulture"" /> if null.</param>
570570
public static bool TryParseUnit(string str, IFormatProvider? provider, out {_unitEnumName} unit)
571571
{{
572-
return UnitParser.Default.TryParse<{_unitEnumName}>(str, provider, out unit);
572+
return UnitsNetSetup.Default.UnitParser.TryParse<{_unitEnumName}>(str, provider, out unit);
573573
}}
574574
575575
#endregion

CodeGen/Generators/UnitsNetGen/StaticQuantityGenerator.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public static bool TryParse(IFormatProvider? formatProvider, Type quantityType,
107107
if (!typeof(IQuantity).IsAssignableFrom(quantityType))
108108
return false;
109109
110-
var parser = QuantityParser.Default;
110+
var parser = UnitsNetSetup.Default.QuantityParser;
111111
112112
return quantityType switch
113113
{");

CodeGen/Generators/UnitsNetGen/UnitTestBaseClassGenerator.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ public string Generate()
9696
using System.Globalization;
9797
using System.Linq;
9898
using System.Threading;
99+
using UnitsNet.Tests.Helpers;
99100
using UnitsNet.Tests.TestsBase;
100101
using UnitsNet.Units;
101102
using Xunit;
@@ -584,7 +585,7 @@ public void HasAtLeastOneAbbreviationSpecified()
584585
var units = Enum.GetValues(typeof({_unitEnumName})).Cast<{_unitEnumName}>();
585586
foreach (var unit in units)
586587
{{
587-
var defaultAbbreviation = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(unit);
588+
var defaultAbbreviation = UnitsNetSetup.Default.UnitAbbreviations.GetDefaultAbbreviation(unit);
588589
}}
589590
}}
590591

README.md

+22-11
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,9 @@ if (Quantity.TryParse(typeof(Length), "3cm", out IQuantity quantity2)
230230
[UnitParser](UnitsNet/CustomCode/UnitParser.cs) parses unit abbreviation strings to unit enum values.
231231

232232
```c#
233-
Enum unit = UnitParser.Default.Parse("cm", typeof(LengthUnit)); // LengthUnit.Centimeter
233+
Enum unit = UnitsNetSetup.Default.UnitParser.Parse("cm", typeof(LengthUnit)); // LengthUnit.Centimeter
234234
235-
if (UnitParser.Default.TryParse("cm", typeof(LengthUnit), out Enum unit2))
235+
if (UnitsNetSetup.Default.UnitParser.TryParse("cm", typeof(LengthUnit), out Enum unit2))
236236
{
237237
// Use unit2 as LengthUnit.Centimeter
238238
}
@@ -277,14 +277,16 @@ Read more at [Extending-with-Custom-Units](https://github.com/angularsen/UnitsNe
277277
#### Map between unit enum values and unit abbreviations
278278
```c#
279279
// Map unit enum values to unit abbreviations
280-
UnitAbbreviationsCache.Default.MapUnitToDefaultAbbreviation(HowMuchUnit.Some, "sm");
281-
UnitAbbreviationsCache.Default.GetDefaultAbbreviation(HowMuchUnit.Some); // "sm"
282-
UnitParser.Default.Parse<HowMuchUnit>("sm"); // HowMuchUnit.Some
280+
var unitAbbreviations = UnitsNetSetup.Default.UnitAbbreviations;
281+
unitAbbreviations.MapUnitToDefaultAbbreviation(HowMuchUnit.Some, "sm");
282+
unitAbbreviations.GetDefaultAbbreviation(HowMuchUnit.Some); // "sm"
283+
284+
UnitsNetSetup.Default.UnitParser.Parse<HowMuchUnit>("sm"); // HowMuchUnit.Some
283285
```
284286

285287
#### Convert between units of custom quantity
286288
```c#
287-
var unitConverter = UnitConverter.Default;
289+
var unitConverter = UnitsNetSetup.Default.UnitConverter;
288290
unitConverter.SetConversionFunction<HowMuch>(HowMuchUnit.Lots, HowMuchUnit.Some, x => new HowMuch(x.Value * 2, HowMuchUnit.Some));
289291
unitConverter.SetConversionFunction<HowMuch>(HowMuchUnit.Tons, HowMuchUnit.Lots, x => new HowMuch(x.Value * 10, HowMuchUnit.Lots));
290292
unitConverter.SetConversionFunction<HowMuch>(HowMuchUnit.Tons, HowMuchUnit.Some, x => new HowMuch(x.Value * 20, HowMuchUnit.Some));
@@ -299,23 +301,32 @@ Console.WriteLine(Convert(HowMuchUnit.Tons)); // 10 tns
299301
```
300302

301303
#### Parse custom quantity
302-
[QuantityParser](UnitsNet/CustomCode/QuantityParser.cs) parses quantity strings to `IQuantity` by providing a `UnitAbbreviationsCache` with custom units and unit abbreviations.
304+
[QuantityParser](UnitsNet/CustomCode/QuantityParser.cs) parses quantity strings to `IQuantity` by mapping custom units to unit abbreviations in `UnitAbbreviationsCache`.
303305

304306
```c#
305-
// Alternatively, manipulate the global UnitAbbreviationsCache.Default.
306-
var unitAbbreviationsCache = new UnitAbbreviationsCache();
307+
// Map custom units to abbreviations
308+
var unitAbbreviationsCache = UnitsNetSetup.Default.UnitAbbreviations;
307309
unitAbbreviationsCache.MapUnitToAbbreviation(HowMuchUnit.Some, "sm");
308310
unitAbbreviationsCache.MapUnitToAbbreviation(HowMuchUnit.ATon, "tn");
309311

310-
var quantityParser = new QuantityParser(unitAbbreviationsCache);
312+
var quantityParser = UnitsNetSetup.Default.QuantityParser;
311313

312-
// 1 Some
314+
// "1 sm" => new HowMuch(1, HowMuchUnit.Some)
313315
HowMuch q = quantityParser.Parse<HowMuch, HowMuchUnit>(
314316
str: "1 sm",
315317
formatProvider: null,
316318
fromDelegate: (value, unit) => new HowMuch((double) value, unit));
317319
```
318320

321+
```c#
322+
// Alternatively, create your own instances to not change the global singleton instances.
323+
var unitAbbreviationsCache = UnitAbbreviationsCache.CreateDefault(); // or .CreateEmpty()
324+
unitAbbreviationsCache.MapUnitToAbbreviation(HowMuchUnit.Some, "sm");
325+
unitAbbreviationsCache.MapUnitToAbbreviation(HowMuchUnit.ATon, "tn");
326+
327+
var quantityParser = new QuantityParser(unitAbbreviationsCache);
328+
```
329+
319330

320331
### Example: Unit converter app
321332
[Source code](https://github.com/angularsen/UnitsNet/tree/master/Samples/UnitConverter.Wpf) for `Samples/UnitConverter.Wpf`<br/>

UnitsNet.Tests/GeneratedCode/TestsBase/AbsorbedDoseOfIonizingRadiationTestsBase.g.cs

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet.Tests/GeneratedCode/TestsBase/AccelerationTestsBase.g.cs

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet.Tests/GeneratedCode/TestsBase/AmountOfSubstanceTestsBase.g.cs

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet.Tests/GeneratedCode/TestsBase/AmplitudeRatioTestsBase.g.cs

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet.Tests/GeneratedCode/TestsBase/AngleTestsBase.g.cs

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet.Tests/GeneratedCode/TestsBase/ApparentEnergyTestsBase.g.cs

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet.Tests/GeneratedCode/TestsBase/ApparentPowerTestsBase.g.cs

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet.Tests/GeneratedCode/TestsBase/AreaDensityTestsBase.g.cs

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet.Tests/GeneratedCode/TestsBase/AreaMomentOfInertiaTestsBase.g.cs

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet.Tests/GeneratedCode/TestsBase/AreaTestsBase.g.cs

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet.Tests/GeneratedCode/TestsBase/BitRateTestsBase.g.cs

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet.Tests/GeneratedCode/TestsBase/BrakeSpecificFuelConsumptionTestsBase.g.cs

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet.Tests/GeneratedCode/TestsBase/CapacitanceTestsBase.g.cs

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet.Tests/GeneratedCode/TestsBase/CoefficientOfThermalExpansionTestsBase.g.cs

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet.Tests/GeneratedCode/TestsBase/CompressibilityTestsBase.g.cs

+2-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)