Skip to content

✨Set IsAotCompatible for net7.0 or later #1543

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
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
11 changes: 11 additions & 0 deletions CodeGen/Generators/UnitsNetGen/StaticQuantityGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ internal static class DefaultProvider
{quantity.Name}.Info,");
Writer.WL(@"
];

internal static void RegisterUnitConversions(UnitConverter unitConverter)
{");
foreach (Quantity quantity in _quantities)
{
Writer.WL($@"
{quantity.Name}.RegisterDefaultConversions(unitConverter);");
}

Writer.WL(@"
}
}
}");
return Writer.ToString();
Expand Down
17 changes: 17 additions & 0 deletions PerfTests/PerfTest_Startup_Aot/PerfTest_Startup_Aot.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>perftest</RootNamespace>

<PublishAot>true</PublishAot>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\UnitsNet\UnitsNet.csproj" />
</ItemGroup>

</Project>
4 changes: 4 additions & 0 deletions PerfTests/PerfTest_Startup_Aot/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
using UnitsNet;
using UnitsNet.Units;

Console.WriteLine(Power.From(5, PowerUnit.Watt));
8 changes: 8 additions & 0 deletions PerfTests/PerfTest_Startup_Aot/profile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env bash
# shellcheck disable=SC2155
declare -r dirname=$(dirname -- "$0")

pushd "$dirname" || exit
dotnet publish
dotnet timeit "$dirname/timeit.json"
popd || exit
8 changes: 8 additions & 0 deletions PerfTests/PerfTest_Startup_Aot/timeit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"warmUpCount": 10,
"count": 20,
"scenarios": [{ "name": "Default" }],
"processName": "../../Artifacts/PerfTest_Startup_Aot/net9.0/win-x64/publish/PerfTest_Startup_Aot.exe",
"workingDirectory": "$(CWD)/",
"processTimeout": 15
}
1 change: 1 addition & 0 deletions UnitsNet.NumberExtensions/UnitsNet.NumberExtensions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<Nullable>enable</Nullable>
<RootNamespace>UnitsNet</RootNamespace>
<TargetFrameworks>netstandard2.0;net8.0;net9.0</TargetFrameworks>
<IsAotCompatible Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))">true</IsAotCompatible>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<Nullable>enable</Nullable>
<RootNamespace>UnitsNet.Serialization.JsonNet</RootNamespace>
<TargetFrameworks>netstandard2.0;net8.0;net9.0</TargetFrameworks>
<IsAotCompatible Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))">true</IsAotCompatible>
</PropertyGroup>

<!-- Strong name signing -->
Expand Down
6 changes: 5 additions & 1 deletion UnitsNet.Serialization.JsonNet/UnitsNetBaseJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using System;
using System.Collections.Concurrent;
using System.Globalization;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
Expand All @@ -15,6 +15,10 @@ namespace UnitsNet.Serialization.JsonNet
/// Contains shared functionality used by <see cref="UnitsNetIQuantityJsonConverter"/> and <see cref="UnitsNetIComparableJsonConverter"/>
/// </summary>
/// <typeparam name="T">The type being converted. Should either be <see cref="IQuantity"/> or <see cref="IComparable"/></typeparam>
#if NET
[RequiresDynamicCode("The native code for this instantiation might not be available at runtime.")]
[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.")]
#endif
public abstract class UnitsNetBaseJsonConverter<T> : NullableQuantityConverter<T>
{
private readonly ConcurrentDictionary<string, (Type Quantity, Type Unit)> _registeredTypes = new();
Expand Down
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.Diagnostics.CodeAnalysis;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

Expand All @@ -15,6 +16,10 @@ namespace UnitsNet.Serialization.JsonNet
/// Should only be used when UnitsNet types are assigned to properties of type IComparable.
/// Requires TypeNameHandling on <see cref="JsonSerializerSettings"/> to be set to something other than <see cref="TypeNameHandling.None"/> so that it outputs $type when serializing.
/// </summary>
#if NET
[RequiresDynamicCode("The native code for this instantiation might not be available at runtime.")]
[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.")]
#endif
public sealed class UnitsNetIComparableJsonConverter : UnitsNetBaseJsonConverter<IComparable?>
{
/// <summary>
Expand Down
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.Diagnostics.CodeAnalysis;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

Expand All @@ -12,6 +13,10 @@ namespace UnitsNet.Serialization.JsonNet
/// JSON.net converter for IQuantity types (e.g. all units in UnitsNet)
/// Use this converter to serialize and deserialize UnitsNet types to and from JSON
/// </summary>
#if NET
[RequiresDynamicCode("The native code for this instantiation might not be available at runtime.")]
[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.")]
#endif
public sealed class UnitsNetIQuantityJsonConverter : UnitsNetBaseJsonConverter<IQuantity?>
{
/// <summary>
Expand Down
7 changes: 7 additions & 0 deletions UnitsNet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "PerfTests", "PerfTests", "{
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PerfTest_Startup_v4_72_0", "PerfTests\PerfTest_Startup_v4_72_0\PerfTest_Startup_v4_72_0.csproj", "{7131F7CC-BD7F-44EB-AD50-AE80CE38F28E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PerfTest_Startup_Aot", "PerfTests\PerfTest_Startup_Aot\PerfTest_Startup_Aot.csproj", "{0222DB9C-52B5-4766-A068-617A44B7E3EB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -120,6 +122,10 @@ Global
{7131F7CC-BD7F-44EB-AD50-AE80CE38F28E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7131F7CC-BD7F-44EB-AD50-AE80CE38F28E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7131F7CC-BD7F-44EB-AD50-AE80CE38F28E}.Release|Any CPU.Build.0 = Release|Any CPU
{0222DB9C-52B5-4766-A068-617A44B7E3EB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0222DB9C-52B5-4766-A068-617A44B7E3EB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0222DB9C-52B5-4766-A068-617A44B7E3EB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0222DB9C-52B5-4766-A068-617A44B7E3EB}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -132,5 +138,6 @@ Global
{BFF3DD22-0F58-4E79-86CD-662D1A174224} = {126F0393-A678-4609-9341-7028F1B2BC2B}
{2E68C361-F6FA-49DC-BB0E-85ADE776391E} = {126F0393-A678-4609-9341-7028F1B2BC2B}
{7131F7CC-BD7F-44EB-AD50-AE80CE38F28E} = {126F0393-A678-4609-9341-7028F1B2BC2B}
{0222DB9C-52B5-4766-A068-617A44B7E3EB} = {126F0393-A678-4609-9341-7028F1B2BC2B}
EndGlobalSection
EndGlobal
5 changes: 2 additions & 3 deletions UnitsNet/CustomCode/Wrappers/ReferencePressure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.

using System;
using System.Linq;
using UnitsNet.CustomCode.Units;
using UnitsNet.InternalHelpers;
using UnitsNet.Units;

namespace UnitsNet.Wrappers
Expand Down Expand Up @@ -35,8 +35,7 @@ public struct ReferencePressure
/// Gets a list of <see cref="PressureReference" /> options: <see cref="PressureReference.Gauge" />,
/// <see cref="PressureReference.Absolute" />, and <see cref="PressureReference.Vacuum" />
/// </summary>
public static PressureReference[] References { get; } =
Enum.GetValues(typeof(PressureReference)).Cast<PressureReference>().ToArray();
public static PressureReference[] References { get; } = EnumHelper.GetValues<PressureReference>();

/// <summary>
/// Initializes a new instance of the <see cref="ReferencePressure" /> struct requiring measured
Expand Down
132 changes: 132 additions & 0 deletions UnitsNet/GeneratedCode/Quantity.g.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions UnitsNet/InternalHelpers/EnumHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Licensed under MIT No Attribution, see LICENSE file at the root.
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.

#if !NET7_0_OR_GREATER
using System.Linq;
#endif

namespace UnitsNet.InternalHelpers;

/// <summary>
/// Helper methods for working with <see cref="Enum"/> types.
/// </summary>
internal static class EnumHelper
{
/// <summary>Retrieves an array of the values of the constants in a specified enumeration type.</summary>
/// <typeparam name="TEnum">The type of the enumeration.</typeparam>
/// <returns>An array that contains the values of the constants in <typeparamref name="TEnum"/>.</returns>
public static TEnum[] GetValues<TEnum>() where TEnum : struct, Enum
{
#if NET7_0_OR_GREATER
return Enum.GetValues<TEnum>();
#else
return Enum.GetValues(typeof(TEnum)).Cast<TEnum>().ToArray();
#endif
}
}
8 changes: 7 additions & 1 deletion UnitsNet/QuantityTypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;

namespace UnitsNet
Expand Down Expand Up @@ -138,7 +139,12 @@ public override bool CanConvertFrom(ITypeDescriptorContext? context, Type source
return (sourceType == typeof(string)) || base.CanConvertFrom(context, sourceType);
}

private static TAttribute? GetAttribute<TAttribute>(ITypeDescriptorContext? context) where TAttribute : UnitAttributeBase
private static TAttribute? GetAttribute<
#if NET
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor | DynamicallyAccessedMemberTypes.PublicFields)]
#endif
TAttribute
>(ITypeDescriptorContext? context) where TAttribute : UnitAttributeBase
{
if (context?.PropertyDescriptor is null) return null;

Expand Down
Loading