Skip to content

Commit a77ffa3

Browse files
committed
Add IsAotCompatible to all projects
- Add IsAotCompatible to UnitsNet - Added EnumHelpers which provides a way to get enum values independent of target platform - Replaced generation of `GetQuantityTypes` with `RegisterDefaultConversions` to avoid reflection - Add IsAotCompatible to UnitsNet.NumberExtensions - Add IsAotCompatible to UnitsNet.JsonNet
1 parent b71a4bf commit a77ffa3

File tree

268 files changed

+1200
-901
lines changed

Some content is hidden

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

268 files changed

+1200
-901
lines changed

CodeGen/Generators/UnitsNetGen/QuantityGenerator.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public string Generate()
4040
using System.Globalization;
4141
using System.Linq;
4242
using System.Runtime.Serialization;
43+
using UnitsNet.InternalHelpers;
4344
using UnitsNet.Units;
4445
#if NET
4546
using System.Numerics;
@@ -154,7 +155,7 @@ private void GenerateStaticConstructor()
154155

155156
Writer.WL($@"
156157
BaseUnit = {_unitEnumName}.{_quantity.BaseUnit};
157-
Units = Enum.GetValues(typeof({_unitEnumName})).Cast<{_unitEnumName}>().ToArray();
158+
Units = EnumHelpers.GetValues<{_unitEnumName}>();
158159
Zero = new {_quantity.Name}(0, BaseUnit);
159160
Info = new QuantityInfo<{_unitEnumName}>(""{_quantity.Name}"",
160161
new UnitInfo<{_unitEnumName}>[]
@@ -305,7 +306,7 @@ private void GenerateProperties()
305306
306307
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
307308
Enum IQuantity.Unit => Unit;
308-
309+
309310
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
310311
UnitKey IQuantity.UnitKey => UnitKey.ForUnit(Unit);
311312
@@ -1195,7 +1196,7 @@ public string ToString(string? format, IFormatProvider? provider)
11951196
#endregion
11961197
" );
11971198
}
1198-
1199+
11991200
/// <inheritdoc cref="GetObsoleteAttributeOrNull(string)"/>
12001201
private static string? GetObsoleteAttributeOrNull(Quantity quantity) => GetObsoleteAttributeOrNull(quantity.ObsoleteText);
12011202

CodeGen/Generators/UnitsNetGen/StaticQuantityGenerator.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,14 @@ public static bool TryParse(IFormatProvider? formatProvider, Type quantityType,
123123
};
124124
}
125125
126-
internal static IEnumerable<Type> GetQuantityTypes()
126+
internal static void RegisterDefaultConversions(UnitConverter unitConverter)
127127
{");
128128
foreach (var quantity in _quantities)
129+
{
129130
Writer.WL($@"
130-
yield return typeof({quantity.Name});");
131+
{quantity.Name}.RegisterDefaultConversions(unitConverter);");
132+
}
133+
131134
Writer.WL(@"
132135
}
133136
}

CodeGen/Generators/UnitsNetGen/UnitTestBaseClassGenerator.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ internal class UnitTestBaseClassGenerator : GeneratorBase
5050
/// Example: "LengthUnit.Centimeter".
5151
/// </summary>
5252
private readonly string _otherOrBaseUnitFullName;
53-
53+
5454
/// <summary>
5555
/// Indicates whether the quantity is dimensionless.
5656
/// </summary>
@@ -178,6 +178,7 @@ public string Generate()
178178
using System.Globalization;
179179
using System.Linq;
180180
using System.Threading;
181+
using UnitsNet.InternalHelpers;
181182
using UnitsNet.Tests.Helpers;
182183
using UnitsNet.Tests.TestsBase;
183184
using UnitsNet.Units;
@@ -374,7 +375,7 @@ public void As_UnitSystem_ReturnsValueInDimensionlessUnit()
374375
var quantity = new {_quantity.Name}(value: 1, unit: {_baseUnitFullName});
375376
376377
var convertedValue = quantity.As(UnitSystem.SI);
377-
378+
378379
Assert.Equal(quantity.Value, convertedValue);
379380
}}
380381
@@ -420,7 +421,7 @@ public void ToUnitSystem_ReturnsValueInDimensionlessUnit()
420421
public void ToUnit_UnitSystem_ThrowsArgumentNullExceptionIfNull()
421422
{{
422423
UnitSystem nullUnitSystem = null!;
423-
Assert.Multiple(() =>
424+
Assert.Multiple(() =>
424425
{{
425426
var quantity = new {_quantity.Name}(value: 1, unit: {_quantity.Name}.BaseUnit);
426427
Assert.Throws<ArgumentNullException>(() => quantity.ToUnit(nullUnitSystem));
@@ -496,23 +497,23 @@ public virtual void ToUnit_UnitSystem_SI_ReturnsQuantityInSIUnits()
496497
IQuantity<{_unitEnumName}> convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
497498
498499
Assert.Equal(expectedUnit, convertedQuantity.Unit);
499-
Assert.Equal(expectedValue, convertedQuantity.Value);
500+
Assert.Equal(expectedValue, convertedQuantity.Value);
500501
}}, () =>
501502
{{
502503
IQuantity quantityToConvert = quantity;
503504
504505
IQuantity convertedQuantity = quantityToConvert.ToUnit(UnitSystem.SI);
505506
506507
Assert.Equal(expectedUnit, convertedQuantity.Unit);
507-
Assert.Equal(expectedValue, convertedQuantity.Value);
508+
Assert.Equal(expectedValue, convertedQuantity.Value);
508509
}});
509510
}}
510511
511512
[Fact]
512513
public void ToUnit_UnitSystem_ThrowsArgumentNullExceptionIfNull()
513514
{{
514515
UnitSystem nullUnitSystem = null!;
515-
Assert.Multiple(() =>
516+
Assert.Multiple(() =>
516517
{{
517518
var quantity = new {_quantity.Name}(value: 1, unit: {_quantity.Name}.BaseUnit);
518519
Assert.Throws<ArgumentNullException>(() => quantity.ToUnit(nullUnitSystem));
@@ -1011,7 +1012,7 @@ public void EqualsReturnsFalseOnNull()
10111012
[Fact]
10121013
public void HasAtLeastOneAbbreviationSpecified()
10131014
{{
1014-
var units = Enum.GetValues(typeof({_unitEnumName})).Cast<{_unitEnumName}>();
1015+
var units = EnumHelpers.GetValues<{_unitEnumName}>();
10151016
foreach (var unit in units)
10161017
{{
10171018
var defaultAbbreviation = UnitsNetSetup.Default.UnitAbbreviations.GetDefaultAbbreviation(unit);

UnitsNet.NumberExtensions/UnitsNet.NumberExtensions.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<Nullable>enable</Nullable>
2424
<RootNamespace>UnitsNet</RootNamespace>
2525
<TargetFrameworks>netstandard2.0;net8.0;net9.0</TargetFrameworks>
26+
<IsAotCompatible Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))">true</IsAotCompatible>
2627
</PropertyGroup>
2728

2829
<ItemGroup>

UnitsNet.Serialization.JsonNet/UnitsNet.Serialization.JsonNet.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<Nullable>enable</Nullable>
2626
<RootNamespace>UnitsNet.Serialization.JsonNet</RootNamespace>
2727
<TargetFrameworks>netstandard2.0;net8.0;net9.0</TargetFrameworks>
28+
<IsAotCompatible Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))">true</IsAotCompatible>
2829
</PropertyGroup>
2930

3031
<!-- Strong name signing -->

UnitsNet.Serialization.JsonNet/UnitsNetBaseJsonConverter.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
using System;
55
using System.Collections.Concurrent;
6-
using System.Globalization;
6+
using System.Diagnostics.CodeAnalysis;
77
using System.Linq;
88
using Newtonsoft.Json;
99
using Newtonsoft.Json.Linq;
@@ -15,6 +15,10 @@ namespace UnitsNet.Serialization.JsonNet
1515
/// Contains shared functionality used by <see cref="UnitsNetIQuantityJsonConverter"/> and <see cref="UnitsNetIComparableJsonConverter"/>
1616
/// </summary>
1717
/// <typeparam name="T">The type being converted. Should either be <see cref="IQuantity"/> or <see cref="IComparable"/></typeparam>
18+
#if NET
19+
[RequiresDynamicCode("The native code for this instantiation might not be available at runtime.")]
20+
[RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")]
21+
#endif
1822
public abstract class UnitsNetBaseJsonConverter<T> : NullableQuantityConverter<T>
1923
{
2024
private readonly ConcurrentDictionary<string, (Type Quantity, Type Unit)> _registeredTypes = new();

UnitsNet.Serialization.JsonNet/UnitsNetIComparableJsonConverter.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
33

44
using System;
5+
using System.Diagnostics.CodeAnalysis;
56
using Newtonsoft.Json;
67
using Newtonsoft.Json.Linq;
78

@@ -15,6 +16,10 @@ namespace UnitsNet.Serialization.JsonNet
1516
/// Should only be used when UnitsNet types are assigned to properties of type IComparable.
1617
/// Requires TypeNameHandling on <see cref="JsonSerializerSettings"/> to be set to something other than <see cref="TypeNameHandling.None"/> so that it outputs $type when serializing.
1718
/// </summary>
19+
#if NET
20+
[RequiresDynamicCode("The native code for this instantiation might not be available at runtime.")]
21+
[RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")]
22+
#endif
1823
public sealed class UnitsNetIComparableJsonConverter : UnitsNetBaseJsonConverter<IComparable?>
1924
{
2025
/// <summary>

UnitsNet.Serialization.JsonNet/UnitsNetIQuantityJsonConverter.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.
33

44
using System;
5+
using System.Diagnostics.CodeAnalysis;
56
using Newtonsoft.Json;
67
using Newtonsoft.Json.Linq;
78

@@ -12,6 +13,10 @@ namespace UnitsNet.Serialization.JsonNet
1213
/// JSON.net converter for IQuantity types (e.g. all units in UnitsNet)
1314
/// Use this converter to serialize and deserialize UnitsNet types to and from JSON
1415
/// </summary>
16+
#if NET
17+
[RequiresDynamicCode("The native code for this instantiation might not be available at runtime.")]
18+
[RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")]
19+
#endif
1520
public sealed class UnitsNetIQuantityJsonConverter : UnitsNetBaseJsonConverter<IQuantity?>
1621
{
1722
/// <summary>

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

Lines changed: 5 additions & 4 deletions
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

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)