Skip to content

Commit

Permalink
Support native AoT (#1517)
Browse files Browse the repository at this point in the history
  • Loading branch information
martincostello authored Jul 19, 2024
1 parent 3e9c701 commit 1dee67d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ namespace Humanizer
}
public static class EnumDehumanizeExtensions
{
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("The native code for the target enumeration might not be available at runtime.")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("The native code for the target enumeration might not be available at runtime.")]
public static System.Enum DehumanizeTo(this string input, System.Type targetEnum, Humanizer.OnNoMatch onNoMatch = 0) { }
public static TTargetEnum DehumanizeTo<[System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembers(System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes.PublicFields)] TTargetEnum>(this string input)
where TTargetEnum : struct, System.Enum { }
Expand Down
4 changes: 4 additions & 0 deletions src/Humanizer/EnumDehumanizeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ public static class EnumDehumanizeExtensions
/// <param name="onNoMatch">What to do when input is not matched to the enum.</param>
/// <exception cref="NoMatchFoundException">Couldn't find any enum member that matches the string</exception>
/// <exception cref="ArgumentException">If targetEnum is not an enum</exception>
#if NET6_0_OR_GREATER
[RequiresDynamicCode("The native code for the target enumeration might not be available at runtime.")]
[RequiresUnreferencedCode("The native code for the target enumeration might not be available at runtime.")]
#endif
public static Enum DehumanizeTo(this string input, Type targetEnum, OnNoMatch onNoMatch = OnNoMatch.ThrowsException)
{
var genericMethod = dehumanizeToMethod.MakeGenericMethod(targetEnum);
Expand Down
3 changes: 3 additions & 0 deletions src/Humanizer/Humanizer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
<PublicKey>0024000004800000940000000602000000240000525341310004000001000100F9104F5F9BDB168AE140366EB1CD84C469B020EA3423D5D29996D5214CE2BD9B7C0BA3EAD1CA545C4399668AB8911E61CC1CC83C7DF6D50FD6B781365B467E65173F40A11C957D27C5AA0CB0F8DA9C91C988203CC8AEF1468C74A472839D0FD870DA8D13A4DC6B3AAFDAF0384D8E18E393C613D88BF02A64467A119902204FCC</PublicKey>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net6.0'))">
<IsAotCompatible>true</IsAotCompatible>
</PropertyGroup>
<ItemGroup>
<Using Remove="System.Net.Http" />
<PackageReference Include="Polyfill" Version="5.6.0" PrivateAssets="all" />
Expand Down
6 changes: 5 additions & 1 deletion src/Humanizer/TimeSpanHumanizeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ public static class TimeSpanHumanizeExtensions
const double _daysInAMonth = _daysInAYear / 12;

static readonly TimeUnit[] _timeUnits = Enum
#if NET6_0_OR_GREATER
.GetValues<TimeUnit>()
#else
.GetValues(typeof(TimeUnit))
.Cast<TimeUnit>()
#endif
.Reverse()
.ToArray();

Expand Down Expand Up @@ -214,4 +218,4 @@ static string ConcatenateTimeSpanParts(IEnumerable<string?> timeSpanParts, Cultu

return string.Join(collectionSeparator, timeSpanParts);
}
}
}

0 comments on commit 1dee67d

Please sign in to comment.