Skip to content

Obsolete GlobalConfiguration #616

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion UnitsNet.Tests/CustomCode/StonePoundsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void StonePoundsToString_FormatsNumberInDefaultCulture()
{
Mass m = Mass.FromStonePounds(3500, 1);
StonePounds stonePounds = m.StonePounds;
string numberInCurrentCulture = 3500.ToString("n0", GlobalConfiguration.DefaultCulture); // Varies between machines, can't hard code it
string numberInCurrentCulture = 3500.ToString("n0", CultureInfo.CurrentUICulture); // Varies between machines, can't hard code it

Assert.Equal($"{numberInCurrentCulture} st 1 lb", stonePounds.ToString());
}
Expand Down
9 changes: 5 additions & 4 deletions UnitsNet.Tests/QuantityIConvertibleTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Globalization;
using UnitsNet.Units;
using Xunit;

Expand Down Expand Up @@ -115,10 +116,10 @@ public void ToSingleTest()
[Fact]
public void ToStringTest()
{
string expected = length.ToString(GlobalConfiguration.DefaultCulture);
Assert.Equal(expected, lengthAsIConvertible.ToString(GlobalConfiguration.DefaultCulture));
Assert.Equal(expected, Convert.ToString(length, GlobalConfiguration.DefaultCulture));
Assert.Equal(expected, Convert.ChangeType(length, typeof(string), GlobalConfiguration.DefaultCulture));
string expected = length.ToString(CultureInfo.CurrentUICulture);
Assert.Equal(expected, lengthAsIConvertible.ToString(CultureInfo.CurrentUICulture));
Assert.Equal(expected, Convert.ToString(length, CultureInfo.CurrentUICulture));
Assert.Equal(expected, Convert.ChangeType(length, typeof(string), CultureInfo.CurrentUICulture));
}

[Fact]
Expand Down
24 changes: 12 additions & 12 deletions UnitsNet.Tests/QuantityTests.ToString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ public void CreatedByCtorWithValueAndUnit_ReturnsValueAndUnit()
[Fact]
public void ReturnsTheOriginalValueAndUnit()
{
var oldCulture = GlobalConfiguration.DefaultCulture;
var oldCulture = CultureInfo.CurrentUICulture;
try
{
GlobalConfiguration.DefaultCulture = CultureInfo.InvariantCulture;
CultureInfo.CurrentUICulture = CultureInfo.InvariantCulture;
Assert.Equal("5 kg", Mass.FromKilograms(5).ToString());
Assert.Equal("5,000 g", Mass.FromGrams(5000).ToString());
Assert.Equal("1e-04 long tn", Mass.FromLongTons(1e-4).ToString());
Expand All @@ -77,59 +77,59 @@ public void ReturnsTheOriginalValueAndUnit()
}
finally
{
GlobalConfiguration.DefaultCulture = oldCulture;
CultureInfo.CurrentUICulture = oldCulture;
}
}

[Fact]
public void ConvertsToTheGivenUnit()
{
var oldCulture = GlobalConfiguration.DefaultCulture;
var oldCulture = CultureInfo.CurrentUICulture;
try
{
GlobalConfiguration.DefaultCulture = CultureInfo.InvariantCulture;
CultureInfo.CurrentUICulture = CultureInfo.InvariantCulture;
Assert.Equal("5,000 g", Mass.FromKilograms(5).ToUnit(MassUnit.Gram).ToString());
Assert.Equal("5 kg", Mass.FromGrams(5000).ToUnit(MassUnit.Kilogram).ToString());
Assert.Equal("0.05 m", Length.FromCentimeters(5).ToUnit(LengthUnit.Meter).ToString());
Assert.Equal("1.97 in", Length.FromCentimeters(5).ToUnit(LengthUnit.Inch).ToString());
}
finally
{
GlobalConfiguration.DefaultCulture = oldCulture;
CultureInfo.CurrentUICulture = oldCulture;
}
}

[Fact]
public void FormatsNumberUsingGivenCulture()
{
var oldCulture = GlobalConfiguration.DefaultCulture;
var oldCulture = CultureInfo.CurrentUICulture;
try
{
GlobalConfiguration.DefaultCulture = CultureInfo.InvariantCulture;
CultureInfo.CurrentUICulture = CultureInfo.InvariantCulture;
Assert.Equal("0.05 m", Length.FromCentimeters(5).ToUnit(LengthUnit.Meter).ToString(null));
Assert.Equal("0.05 m", Length.FromCentimeters(5).ToUnit(LengthUnit.Meter).ToString(CultureInfo.InvariantCulture));
Assert.Equal("0,05 m", Length.FromCentimeters(5).ToUnit(LengthUnit.Meter).ToString(new CultureInfo("nb-NO")));
}
finally
{
GlobalConfiguration.DefaultCulture = oldCulture;
CultureInfo.CurrentUICulture = oldCulture;
}
}

[Fact]
public void FormatsNumberUsingGivenDigitsAfterRadix()
{
var oldCulture = GlobalConfiguration.DefaultCulture;
var oldCulture = CultureInfo.CurrentUICulture;
try
{
GlobalConfiguration.DefaultCulture = CultureInfo.InvariantCulture;
CultureInfo.CurrentUICulture = CultureInfo.InvariantCulture;
Assert.Equal("0.05 m", Length.FromCentimeters(5).ToUnit(LengthUnit.Meter).ToString(null, 4));
Assert.Equal("1.97 in", Length.FromCentimeters(5).ToUnit(LengthUnit.Inch).ToString(null, 2));
Assert.Equal("1.9685 in", Length.FromCentimeters(5).ToUnit(LengthUnit.Inch).ToString(null, 4));
}
finally
{
GlobalConfiguration.DefaultCulture = oldCulture;
CultureInfo.CurrentUICulture = oldCulture;
}
}
}
Expand Down
12 changes: 9 additions & 3 deletions UnitsNet/CustomCode/GlobalConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Globalization;
using System.Threading;

// ReSharper disable once CheckNamespace
namespace UnitsNet
Expand All @@ -11,12 +12,17 @@ namespace UnitsNet
/// Global configuration for culture, used as default culture in methods like <see cref="Length.ToString()" /> and
/// <see cref="Length.Parse(string)" />.
/// </summary>
[Obsolete("The only property DefaultCulture is now deprecated. Manipulate Thread.CurrentThread.CurrentUICulture instead.")]
public static class GlobalConfiguration
{
/// <summary>
/// Defaults to <see cref="CultureInfo.CurrentUICulture" /> when creating an instance with no culture provided.
/// Can be overridden, but note that this is static and will affect all subsequent usages.
/// Wrapper for <see cref="Thread.CurrentUICulture"/>.
/// </summary>
public static IFormatProvider DefaultCulture { get; set; } = CultureInfo.CurrentUICulture;
[Obsolete("Manipulate Thread.CurrentThread.CurrentUICulture instead, this property will be removed.")]
public static IFormatProvider DefaultCulture
{
get => Thread.CurrentThread.CurrentUICulture;
set => Thread.CurrentThread.CurrentUICulture = (CultureInfo) value;
}
}
}
3 changes: 2 additions & 1 deletion UnitsNet/CustomCode/Quantities/Length.extra.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.

using System;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Threading;
using JetBrains.Annotations;
Expand Down Expand Up @@ -207,7 +208,7 @@ public override string ToString()
/// </param>
public string ToString([CanBeNull] IFormatProvider cultureInfo)
{
cultureInfo = cultureInfo ?? GlobalConfiguration.DefaultCulture;
cultureInfo = cultureInfo ?? CultureInfo.CurrentUICulture;

var footUnit = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(LengthUnit.Foot, cultureInfo);
var inchUnit = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(LengthUnit.Inch, cultureInfo);
Expand Down
3 changes: 2 additions & 1 deletion UnitsNet/CustomCode/Quantities/Mass.extra.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Threading;
using System.Globalization;
using JetBrains.Annotations;
using UnitsNet.Units;

Expand Down Expand Up @@ -124,7 +125,7 @@ public override string ToString()
/// </param>
public string ToString([CanBeNull] IFormatProvider cultureInfo)
{
cultureInfo = cultureInfo ?? GlobalConfiguration.DefaultCulture;
cultureInfo = cultureInfo ?? CultureInfo.CurrentUICulture;

var stoneUnit = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(MassUnit.Stone, cultureInfo);
var poundUnit = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(MassUnit.Pound, cultureInfo);
Expand Down
30 changes: 15 additions & 15 deletions UnitsNet/CustomCode/UnitAbbreviationsCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private void LoadGeneratedAbbreviations()
[PublicAPI]
public void MapUnitToAbbreviation<TUnitType>(TUnitType unit, params string[] abbreviations) where TUnitType : Enum
{
MapUnitToAbbreviation(typeof(TUnitType), Convert.ToInt32(unit), GlobalConfiguration.DefaultCulture, abbreviations);
MapUnitToAbbreviation(typeof(TUnitType), Convert.ToInt32(unit), CultureInfo.CurrentUICulture, abbreviations);
}

/// <summary>
Expand All @@ -86,7 +86,7 @@ public void MapUnitToAbbreviation<TUnitType>(TUnitType unit, params string[] abb
/// <typeparam name="TUnitType">The type of unit enum.</typeparam>
public void MapUnitToDefaultAbbreviation<TUnitType>(TUnitType unit, string abbreviation) where TUnitType : Enum
{
MapUnitToDefaultAbbreviation(typeof(TUnitType), Convert.ToInt32(unit), GlobalConfiguration.DefaultCulture, abbreviation);
MapUnitToDefaultAbbreviation(typeof(TUnitType), Convert.ToInt32(unit), CultureInfo.CurrentUICulture, abbreviation);
}

/// <summary>
Expand All @@ -95,7 +95,7 @@ public void MapUnitToDefaultAbbreviation<TUnitType>(TUnitType unit, string abbre
/// in order to <see cref="UnitParser.Parse{TUnitType}"/> or <see cref="GetDefaultAbbreviation{TUnitType}"/> on them later.
/// </summary>
/// <param name="unit">The unit enum value.</param>
/// <param name="formatProvider">The format provider to use for lookup. Defaults to <see cref="GlobalConfiguration.DefaultCulture" /> if null.</param>
/// <param name="formatProvider">The format provider to use for lookup. Defaults to <see cref="CultureInfo.CurrentUICulture" /> if null.</param>
/// <param name="abbreviations">Unit abbreviations to add.</param>
/// <typeparam name="TUnitType">The type of unit enum.</typeparam>
[PublicAPI]
Expand All @@ -116,7 +116,7 @@ public void MapUnitToAbbreviation<TUnitType>(TUnitType unit, IFormatProvider for
/// in order to <see cref="UnitParser.Parse{TUnitType}"/> or <see cref="GetDefaultAbbreviation{TUnitType}"/> on them later.
/// </summary>
/// <param name="unit">The unit enum value.</param>
/// <param name="formatProvider">The format provider to use for lookup. Defaults to <see cref="GlobalConfiguration.DefaultCulture" /> if null.</param>
/// <param name="formatProvider">The format provider to use for lookup. Defaults to <see cref="CultureInfo.CurrentUICulture" /> if null.</param>
/// <param name="abbreviation">Unit abbreviation to add as default.</param>
/// <typeparam name="TUnitType">The type of unit enum.</typeparam>
[PublicAPI]
Expand All @@ -138,7 +138,7 @@ public void MapUnitToDefaultAbbreviation<TUnitType>(TUnitType unit, IFormatProvi
/// </summary>
/// <param name="unitType">The unit enum type.</param>
/// <param name="unitValue">The unit enum value.</param>
/// <param name="formatProvider">The format provider to use for lookup. Defaults to <see cref="GlobalConfiguration.DefaultCulture" /> if null.</param>
/// <param name="formatProvider">The format provider to use for lookup. Defaults to <see cref="CultureInfo.CurrentUICulture" /> if null.</param>
/// <param name="abbreviations">Unit abbreviations to add.</param>
[PublicAPI]
public void MapUnitToAbbreviation(Type unitType, int unitValue, IFormatProvider formatProvider, [NotNull] params string[] abbreviations)
Expand All @@ -153,7 +153,7 @@ public void MapUnitToAbbreviation(Type unitType, int unitValue, IFormatProvider
/// </summary>
/// <param name="unitType">The unit enum type.</param>
/// <param name="unitValue">The unit enum value.</param>
/// <param name="formatProvider">The format provider to use for lookup. Defaults to <see cref="GlobalConfiguration.DefaultCulture" /> if null.</param>
/// <param name="formatProvider">The format provider to use for lookup. Defaults to <see cref="CultureInfo.CurrentUICulture" /> if null.</param>
/// <param name="abbreviation">Unit abbreviation to add as default.</param>
[PublicAPI]
public void MapUnitToDefaultAbbreviation(Type unitType, int unitValue, IFormatProvider formatProvider, [NotNull] string abbreviation)
Expand All @@ -169,7 +169,7 @@ private void PerformAbbreviationMapping(Type unitType, int unitValue, IFormatPro
if (abbreviations == null)
throw new ArgumentNullException(nameof(abbreviations));

formatProvider = formatProvider ?? GlobalConfiguration.DefaultCulture;
formatProvider = formatProvider ?? CultureInfo.CurrentUICulture;

if (!_lookupsForCulture.TryGetValue(formatProvider, out var quantitiesForProvider))
quantitiesForProvider = _lookupsForCulture[formatProvider] = new UnitTypeToLookup();
Expand All @@ -188,7 +188,7 @@ private void PerformAbbreviationMapping(Type unitType, int unitValue, IFormatPro
/// Example: GetDefaultAbbreviation&lt;LengthUnit&gt;(LengthUnit.Kilometer) => "km"
/// </summary>
/// <param name="unit">The unit enum value.</param>
/// <param name="formatProvider">The format provider to use for lookup. Defaults to <see cref="GlobalConfiguration.DefaultCulture" /> if null.</param>
/// <param name="formatProvider">The format provider to use for lookup. Defaults to <see cref="CultureInfo.CurrentUICulture" /> if null.</param>
/// <typeparam name="TUnitType">The type of unit enum.</typeparam>
/// <returns>The default unit abbreviation string.</returns>
[PublicAPI]
Expand Down Expand Up @@ -223,7 +223,7 @@ public string GetDefaultAbbreviation<TUnitType>(TUnitType unit, IFormatProvider
/// </summary>
/// <param name="unitType">The unit enum type.</param>
/// <param name="unitValue">The unit enum value.</param>
/// <param name="formatProvider">The format provider to use for lookup. Defaults to <see cref="GlobalConfiguration.DefaultCulture" /> if null.</param>
/// <param name="formatProvider">The format provider to use for lookup. Defaults to <see cref="CultureInfo.CurrentUICulture" /> if null.</param>
/// <returns>The default unit abbreviation string.</returns>
[PublicAPI]
public string GetDefaultAbbreviation(Type unitType, int unitValue, IFormatProvider formatProvider = null)
Expand Down Expand Up @@ -253,7 +253,7 @@ public string GetDefaultAbbreviation(Type unitType, int unitValue, IFormatProvid
/// </summary>
/// <typeparam name="TUnitType">Enum type for units.</typeparam>
/// <param name="unit">Enum value for unit.</param>
/// <param name="formatProvider">The format provider to use for lookup. Defaults to <see cref="GlobalConfiguration.DefaultCulture" /> if null.</param>
/// <param name="formatProvider">The format provider to use for lookup. Defaults to <see cref="CultureInfo.CurrentUICulture" /> if null.</param>
/// <returns>Unit abbreviations associated with unit.</returns>
[PublicAPI]
public string[] GetUnitAbbreviations<TUnitType>(TUnitType unit, IFormatProvider formatProvider = null) where TUnitType : Enum
Expand All @@ -266,12 +266,12 @@ public string[] GetUnitAbbreviations<TUnitType>(TUnitType unit, IFormatProvider
/// </summary>
/// <param name="unitType">Enum type for unit.</param>
/// <param name="unitValue">Enum value for unit.</param>
/// <param name="formatProvider">The format provider to use for lookup. Defaults to <see cref="GlobalConfiguration.DefaultCulture" /> if null.</param>
/// <param name="formatProvider">The format provider to use for lookup. Defaults to <see cref="CultureInfo.CurrentUICulture" /> if null.</param>
/// <returns>Unit abbreviations associated with unit.</returns>
[PublicAPI]
public string[] GetUnitAbbreviations(Type unitType, int unitValue, IFormatProvider formatProvider = null)
{
formatProvider = formatProvider ?? GlobalConfiguration.DefaultCulture;
formatProvider = formatProvider ?? CultureInfo.CurrentUICulture;

if(!TryGetUnitValueAbbreviationLookup(unitType, formatProvider, out var lookup))
return formatProvider != FallbackCulture ? GetUnitAbbreviations(unitType, unitValue, FallbackCulture) : new string[] { };
Expand All @@ -287,12 +287,12 @@ public string[] GetUnitAbbreviations(Type unitType, int unitValue, IFormatProvid
/// Get all abbreviations for all units of a quantity.
/// </summary>
/// <param name="unitEnumType">Enum type for unit.</param>
/// <param name="formatProvider">The format provider to use for lookup. Defaults to <see cref="GlobalConfiguration.DefaultCulture" /> if null.</param>
/// <param name="formatProvider">The format provider to use for lookup. Defaults to <see cref="CultureInfo.CurrentUICulture" /> if null.</param>
/// <returns>Unit abbreviations associated with unit.</returns>
[PublicAPI]
public string[] GetAllUnitAbbreviationsForQuantity(Type unitEnumType, IFormatProvider formatProvider = null)
{
formatProvider = formatProvider ?? GlobalConfiguration.DefaultCulture;
formatProvider = formatProvider ?? CultureInfo.CurrentUICulture;

if(!TryGetUnitValueAbbreviationLookup(unitEnumType, formatProvider, out var lookup))
return formatProvider != FallbackCulture ? GetAllUnitAbbreviationsForQuantity(unitEnumType, FallbackCulture) : new string[] { };
Expand All @@ -304,7 +304,7 @@ internal bool TryGetUnitValueAbbreviationLookup(Type unitType, IFormatProvider f
{
unitToAbbreviations = null;

formatProvider = formatProvider ?? GlobalConfiguration.DefaultCulture;
formatProvider = formatProvider ?? CultureInfo.CurrentUICulture;

if(!_lookupsForCulture.TryGetValue(formatProvider, out var quantitiesForProvider))
return formatProvider != FallbackCulture ? TryGetUnitValueAbbreviationLookup(unitType, FallbackCulture, out unitToAbbreviations) : false;
Expand Down
Loading