Skip to content

Commit

Permalink
add enum polyfill (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Feb 6, 2024
1 parent 6477108 commit 137884e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project>
<PropertyGroup>
<Version>2.1.0</Version>
<Version>2.2.0</Version>
<AssemblyVersion>1.0.0</AssemblyVersion>
<PackageTags>Polyfill</PackageTags>
<DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports>
Expand Down
46 changes: 46 additions & 0 deletions src/Polyfill/EnumPolyfill.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// <auto-generated />

#pragma warning disable

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Link = System.ComponentModel.DescriptionAttribute;

#if PolyPublic
public
#endif
static partial class EnumPolyfill
{
/// <summary>
/// Retrieves an array of the values of the constants in a specified enumeration type.
/// </summary>
/// <returns>An array that contains the values of the constants in TEnum.</returns>
[Link("https://learn.microsoft.com/en-us/dotnet/api/system.enum.getvalues")]
public static TEnum[] GetValues<TEnum>()
where TEnum : struct, Enum
{
#if NETCOREAPPX || NETFRAMEWORK || NETSTANDARD
var values = Enum.GetValues(typeof(TEnum));
var result = new TEnum[values.Length];
Array.Copy(values, result, values.Length);
return result;
#else
return Enum.GetValues<TEnum>();
#endif
}
/// <summary>
/// Retrieves an array of the names of the constants in a specified enumeration type.
/// </summary>
/// <returns>A string array of the names of the constants in TEnum.</returns>
[Link("https://learn.microsoft.com/en-us/dotnet/api/system.enum.getnames")]
public static string[] GetNames<TEnum>()
where TEnum : struct, Enum
{
#if NETCOREAPPX || NETFRAMEWORK || NETSTANDARD
return Enum.GetNames(typeof(TEnum));
#else
return Enum.GetNames<TEnum>();
#endif
}
}
16 changes: 16 additions & 0 deletions src/Tests/PolyfillTests_Enum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
partial class PolyfillTests
{
[Test]
public void EnumGetValues()
{
var dayOfWeeks = EnumPolyfill.GetValues<DayOfWeek>();
Assert.AreEqual(DayOfWeek.Sunday, dayOfWeeks[0]);
}

[Test]
public void EnumGetNames()
{
var dayOfWeeks = EnumPolyfill.GetNames<DayOfWeek>();
Assert.AreEqual("Sunday", dayOfWeeks[0]);
}
}

0 comments on commit 137884e

Please sign in to comment.