Skip to content
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 Build/build-functions.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ $nuget = "$root\Tools\NuGet.exe"
$vswhere = "${env:ProgramFiles(x86)}\Microsoft Visual Studio\Installer\vswhere.exe"
$msbuild = & $vswhere -latest -products * -requires Microsoft.Component.MSBuild -property installationPath
if ($msbuild) {
$msbuild = join-path $msbuild 'MSBuild\15.0\Bin\MSBuild.exe'
$msbuild = join-path $msbuild 'MSBuild\Current\Bin\MSBuild.exe'
}

function Remove-ArtifactsDir {
Expand Down
34 changes: 18 additions & 16 deletions CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public override string Generate()
using UnitsNet.InternalHelpers;
using UnitsNet.Units;

#nullable enable

// ReSharper disable once CheckNamespace

namespace UnitsNet
Expand Down Expand Up @@ -186,7 +188,7 @@ private void GenerateInstanceConstructors()
/// <exception cref=""ArgumentException"">No unit was found for the given <see cref=""UnitSystem""/>.</exception>
public {_quantity.Name}({_valueType} value, UnitSystem unitSystem)
{{
if(unitSystem == null) throw new ArgumentNullException(nameof(unitSystem));
if(unitSystem is null) throw new ArgumentNullException(nameof(unitSystem));

var unitInfos = Info.GetUnitInfosFor(unitSystem.BaseUnits);
var firstUnitInfo = unitInfos.FirstOrDefault();
Expand Down Expand Up @@ -338,7 +340,7 @@ public static string GetAbbreviation({_unitEnumName} unit)
/// <param name=""unit"">Unit to get abbreviation for.</param>
/// <returns>Unit abbreviation string.</returns>
/// <param name=""provider"">Format to use for localization. Defaults to <see cref=""CultureInfo.CurrentUICulture"" /> if null.</param>
public static string GetAbbreviation({_unitEnumName} unit, [CanBeNull] IFormatProvider provider)
public static string GetAbbreviation({_unitEnumName} unit, IFormatProvider? provider)
{{
return UnitAbbreviationsCache.Default.GetDefaultAbbreviation(unit, provider);
}}
Expand Down Expand Up @@ -441,7 +443,7 @@ private void GenerateStaticParseMethods()
/// Units.NET exceptions from other exceptions.
/// </exception>
/// <param name=""provider"">Format to use when parsing number and unit. Defaults to <see cref=""CultureInfo.CurrentUICulture"" /> if null.</param>
public static {_quantity.Name} Parse(string str, [CanBeNull] IFormatProvider provider)
public static {_quantity.Name} Parse(string str, IFormatProvider? provider)
{{
return QuantityParser.Default.Parse<{_quantity.Name}, {_unitEnumName}>(
str,
Expand All @@ -457,7 +459,7 @@ private void GenerateStaticParseMethods()
/// <example>
/// Length.Parse(""5.5 m"", new CultureInfo(""en-US""));
/// </example>
public static bool TryParse([CanBeNull] string str, out {_quantity.Name} result)
public static bool TryParse(string? str, out {_quantity.Name} result)
{{
return TryParse(str, null, out result);
}}
Expand All @@ -472,7 +474,7 @@ public static bool TryParse([CanBeNull] string str, out {_quantity.Name} result)
/// Length.Parse(""5.5 m"", new CultureInfo(""en-US""));
/// </example>
/// <param name=""provider"">Format to use when parsing number and unit. Defaults to <see cref=""CultureInfo.CurrentUICulture"" /> if null.</param>
public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider provider, out {_quantity.Name} result)
public static bool TryParse(string? str, IFormatProvider? provider, out {_quantity.Name} result)
{{
return QuantityParser.Default.TryParse<{_quantity.Name}, {_unitEnumName}>(
str,
Expand Down Expand Up @@ -505,7 +507,7 @@ public static bool TryParse([CanBeNull] string str, [CanBeNull] IFormatProvider
/// </example>
/// <exception cref=""ArgumentNullException"">The value of 'str' cannot be null. </exception>
/// <exception cref=""UnitsNetException"">Error parsing string.</exception>
public static {_unitEnumName} ParseUnit(string str, [CanBeNull] IFormatProvider provider)
public static {_unitEnumName} ParseUnit(string str, IFormatProvider? provider)
{{
return UnitParser.Default.Parse<{_unitEnumName}>(str, provider);
}}
Expand All @@ -526,7 +528,7 @@ public static bool TryParseUnit(string str, out {_unitEnumName} unit)
/// Length.TryParseUnit(""m"", new CultureInfo(""en-US""));
/// </example>
/// <param name=""provider"">Format to use when parsing number and unit. Defaults to <see cref=""CultureInfo.CurrentUICulture"" /> if null.</param>
public static bool TryParseUnit(string str, IFormatProvider provider, out {_unitEnumName} unit)
public static bool TryParseUnit(string str, IFormatProvider? provider, out {_unitEnumName} unit)
{{
return UnitParser.Default.TryParse<{_unitEnumName}>(str, provider, out unit);
}}
Expand Down Expand Up @@ -818,7 +820,7 @@ public double As({_unitEnumName} unit)
/// <inheritdoc cref=""IQuantity.As(UnitSystem)""/>
public double As(UnitSystem unitSystem)
{{
if(unitSystem == null)
if(unitSystem is null)
throw new ArgumentNullException(nameof(unitSystem));

var unitInfos = Info.GetUnitInfosFor(unitSystem.BaseUnits);
Expand Down Expand Up @@ -861,7 +863,7 @@ IQuantity IQuantity.ToUnit(Enum unit)
/// <inheritdoc cref=""IQuantity.ToUnit(UnitSystem)""/>
public {_quantity.Name} ToUnit(UnitSystem unitSystem)
{{
if(unitSystem == null)
if(unitSystem is null)
throw new ArgumentNullException(nameof(unitSystem));

var unitInfos = Info.GetUnitInfosFor(unitSystem.BaseUnits);
Expand Down Expand Up @@ -960,7 +962,7 @@ public override string ToString()
/// </summary>
/// <returns>String representation.</returns>
/// <param name=""provider"">Format to use for localization and number formatting. Defaults to <see cref=""CultureInfo.CurrentUICulture"" /> if null.</param>
public string ToString([CanBeNull] IFormatProvider provider)
public string ToString(IFormatProvider? provider)
{{
return ToString(""g"", provider);
}}
Expand All @@ -972,7 +974,7 @@ public string ToString([CanBeNull] IFormatProvider provider)
/// <returns>String representation.</returns>
/// <param name=""provider"">Format to use for localization and number formatting. Defaults to <see cref=""CultureInfo.CurrentUICulture"" /> if null.</param>
[Obsolete(@""This method is deprecated and will be removed at a future release. Please use ToString(""""s2"""") or ToString(""""s2"""", provider) where 2 is an example of the number passed to significantDigitsAfterRadix."")]
public string ToString([CanBeNull] IFormatProvider provider, int significantDigitsAfterRadix)
public string ToString(IFormatProvider? provider, int significantDigitsAfterRadix)
{{
var value = Convert.ToDouble(Value);
var format = UnitFormatter.GetFormat(value, significantDigitsAfterRadix);
Expand All @@ -987,7 +989,7 @@ public string ToString([CanBeNull] IFormatProvider provider, int significantDigi
/// <returns>String representation.</returns>
/// <param name=""provider"">Format to use for localization and number formatting. Defaults to <see cref=""CultureInfo.CurrentUICulture"" /> if null.</param>
[Obsolete(""This method is deprecated and will be removed at a future release. Please use string.Format()."")]
public string ToString([CanBeNull] IFormatProvider provider, [NotNull] string format, [NotNull] params object[] args)
public string ToString(IFormatProvider? provider, [NotNull] string format, [NotNull] params object[] args)
{{
if (format == null) throw new ArgumentNullException(nameof(format));
if (args == null) throw new ArgumentNullException(nameof(args));
Expand Down Expand Up @@ -1015,15 +1017,15 @@ public string ToString(string format)
/// Gets the string representation of this instance in the specified format string using the specified format provider, or <see cref=""CultureInfo.CurrentUICulture"" /> if null.
/// </summary>
/// <param name=""format"">The format string.</param>
/// <param name=""formatProvider"">Format to use for localization and number formatting. Defaults to <see cref=""CultureInfo.CurrentUICulture"" /> if null.</param>
/// <param name=""provider"">Format to use for localization and number formatting. Defaults to <see cref=""CultureInfo.CurrentUICulture"" /> if null.</param>
/// <returns>The string representation.</returns>
public string ToString(string format, IFormatProvider formatProvider)
public string ToString(string format, IFormatProvider? provider)
{{
return QuantityFormatter.Format<{_unitEnumName}>(this, format, formatProvider);
return QuantityFormatter.Format<{_unitEnumName}>(this, format, provider);
}}

#endregion
");
" );
}

private void GenerateIConvertibleMethods()
Expand Down
6 changes: 4 additions & 2 deletions CodeGen/Generators/UnitsNetGen/StaticQuantityGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public override string Generate()
using UnitsNet.InternalHelpers;
using UnitsNet.Units;

#nullable enable

namespace UnitsNet
{
/// <summary>
Expand Down Expand Up @@ -60,7 +62,7 @@ public static IQuantity FromQuantityType(QuantityType quantityType, QuantityValu
/// <param name=""unit"">Unit enum value.</param>
/// <param name=""quantity"">The resulting quantity if successful, otherwise <c>default</c>.</param>
/// <returns><c>True</c> if successful with <paramref name=""quantity""/> assigned the value, otherwise <c>false</c>.</returns>
public static bool TryFrom(QuantityValue value, Enum unit, out IQuantity quantity)
public static bool TryFrom(QuantityValue value, Enum unit, out IQuantity? quantity)
{
switch (unit)
{");
Expand Down Expand Up @@ -92,7 +94,7 @@ public static bool TryFrom(QuantityValue value, Enum unit, out IQuantity quantit
/// <param name=""quantityString"">Quantity string representation, such as ""1.5 kg"". Must be compatible with given quantity type.</param>
/// <param name=""quantity"">The resulting quantity if successful, otherwise <c>default</c>.</param>
/// <returns>The parsed quantity.</returns>
public static bool TryParse([CanBeNull] IFormatProvider formatProvider, Type quantityType, string quantityString, out IQuantity quantity)
public static bool TryParse(IFormatProvider? formatProvider, Type quantityType, string quantityString, out IQuantity? quantity)
{
quantity = default(IQuantity);

Expand Down
6 changes: 3 additions & 3 deletions UnitsNet/CustomCode/Quantities/Length.extra.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static Length FromFeetInches(double feet, double inches)
/// <param name="str"></param>
/// <param name="formatProvider">Optionally specify the culture format numbers and localize unit abbreviations. Defaults to thread's culture.</param>
/// <returns>Parsed length.</returns>
public static Length ParseFeetInches([NotNull] string str, IFormatProvider formatProvider = null)
public static Length ParseFeetInches([NotNull] string str, IFormatProvider? formatProvider = null)
{
if (str == null) throw new ArgumentNullException(nameof(str));
if (!TryParseFeetInches(str, out Length result, formatProvider))
Expand All @@ -69,7 +69,7 @@ public static Length ParseFeetInches([NotNull] string str, IFormatProvider forma
/// <param name="str"></param>
/// <param name="result">Parsed length.</param>
/// <param name="formatProvider">Optionally specify the culture format numbers and localize unit abbreviations. Defaults to thread's culture.</param>
public static bool TryParseFeetInches([CanBeNull] string str, out Length result, IFormatProvider formatProvider = null)
public static bool TryParseFeetInches(string? str, out Length result, IFormatProvider? formatProvider = null)
{
if (str == null)
{
Expand Down Expand Up @@ -213,7 +213,7 @@ public override string ToString()
/// Optional culture to format number and localize unit abbreviations.
/// If null, defaults to <see cref="Thread.CurrentUICulture"/>.
/// </param>
public string ToString([CanBeNull] IFormatProvider cultureInfo)
public string ToString(IFormatProvider? cultureInfo)
{
cultureInfo = cultureInfo ?? CultureInfo.CurrentUICulture;

Expand Down
2 changes: 1 addition & 1 deletion UnitsNet/CustomCode/Quantities/Mass.extra.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public override string ToString()
/// Optional culture to format number and localize unit abbreviations.
/// If null, defaults to <see cref="Thread.CurrentUICulture"/>.
/// </param>
public string ToString([CanBeNull] IFormatProvider cultureInfo)
public string ToString(IFormatProvider? cultureInfo)
{
cultureInfo = cultureInfo ?? CultureInfo.CurrentUICulture;

Expand Down
13 changes: 7 additions & 6 deletions UnitsNet/CustomCode/Quantity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ static Quantity()
/// <exception cref="ArgumentException">Unit value is not a know unit enum type.</exception>
public static IQuantity From(QuantityValue value, Enum unit)
{
if (TryFrom(value, unit, out IQuantity quantity))
return quantity;
if (TryFrom(value, unit, out IQuantity? quantity))
return quantity!;

throw new ArgumentException(
$"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?");
}

/// <inheritdoc cref="TryFrom(QuantityValue,System.Enum,out UnitsNet.IQuantity)"/>
public static bool TryFrom(double value, Enum unit, out IQuantity quantity)
public static bool TryFrom(double value, Enum unit, out IQuantity? quantity)
{
// Implicit cast to QuantityValue would prevent TryFrom from being called,
// so we need to explicitly check this here for double arguments.
Expand All @@ -79,18 +79,19 @@ public static bool TryFrom(double value, Enum unit, out IQuantity quantity)
/// <param name="quantityString">Quantity string representation, such as "1.5 kg". Must be compatible with given quantity type.</param>
/// <returns>The parsed quantity.</returns>
/// <exception cref="ArgumentException">Type must be of type UnitsNet.IQuantity -or- Type is not a known quantity type.</exception>
public static IQuantity Parse([CanBeNull] IFormatProvider formatProvider, Type quantityType, string quantityString)
public static IQuantity Parse(IFormatProvider? formatProvider, Type quantityType, string quantityString)
{
if (!typeof(IQuantity).Wrap().IsAssignableFrom(quantityType))
throw new ArgumentException($"Type {quantityType} must be of type UnitsNet.IQuantity.");

if (TryParse(formatProvider, quantityType, quantityString, out IQuantity quantity)) return quantity;
if (TryParse(formatProvider, quantityType, quantityString, out IQuantity? quantity))
return quantity!;

throw new ArgumentException($"Quantity string could not be parsed to quantity {quantityType}.");
}

/// <inheritdoc cref="TryParse(IFormatProvider,System.Type,string,out UnitsNet.IQuantity)"/>
public static bool TryParse(Type quantityType, string quantityString, out IQuantity quantity) =>
public static bool TryParse(Type quantityType, string quantityString, out IQuantity? quantity) =>
TryParse(null, quantityType, quantityString, out quantity);

/// <summary>
Expand Down
Loading