diff --git a/PolyShim.Tests/Net50/EnumTests.cs b/PolyShim.Tests/Net50/EnumTests.cs new file mode 100644 index 0000000..f763cc1 --- /dev/null +++ b/PolyShim.Tests/Net50/EnumTests.cs @@ -0,0 +1,34 @@ +using System; +using FluentAssertions; +using Xunit; + +namespace PolyShim.Tests.Net50; + +public class EnumTests +{ + [Fact] + public void GetNames_Test() + { + // Act & assert + Enum.GetNames() + .Should() + .Equal("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"); + } + + [Fact] + public void GetValues_Test() + { + // Act & assert + Enum.GetValues() + .Should() + .Equal( + DayOfWeek.Sunday, + DayOfWeek.Monday, + DayOfWeek.Tuesday, + DayOfWeek.Wednesday, + DayOfWeek.Thursday, + DayOfWeek.Friday, + DayOfWeek.Saturday + ); + } +} diff --git a/PolyShim.Tests/NetCore20/EnumTests.cs b/PolyShim.Tests/NetCore20/EnumTests.cs new file mode 100644 index 0000000..7a6b481 --- /dev/null +++ b/PolyShim.Tests/NetCore20/EnumTests.cs @@ -0,0 +1,18 @@ +using System; +using FluentAssertions; +using Xunit; + +namespace PolyShim.Tests.NetCore20; + +public class EnumTests +{ + [Fact] + public void Parse_Test() + { + // Act & assert + Enum.Parse("Friday").Should().Be(DayOfWeek.Friday); + Enum.Parse("friday", true).Should().Be(DayOfWeek.Friday); + Assert.Throws(() => Enum.Parse("Moonday")); + Assert.Throws(() => Enum.Parse("friday", false)); + } +} diff --git a/PolyShim/Net50/Enum.cs b/PolyShim/Net50/Enum.cs new file mode 100644 index 0000000..225ea85 --- /dev/null +++ b/PolyShim/Net50/Enum.cs @@ -0,0 +1,23 @@ +#if (NETCOREAPP && !NET5_0_OR_GREATER) || (NETFRAMEWORK) || (NETSTANDARD) +#nullable enable +// ReSharper disable RedundantUsingDirective +// ReSharper disable CheckNamespace +// ReSharper disable InconsistentNaming +// ReSharper disable PartialTypeWithSinglePart + +using System; + +internal static partial class PolyfillExtensions +{ + extension(Enum) + { + // https://learn.microsoft.com/dotnet/api/system.enum.getnames#system-enum-getnames-1 + public static string[] GetNames() + where T : struct, Enum => Enum.GetNames(typeof(T)); + + // https://learn.microsoft.com/dotnet/api/system.enum.getvalues#system-enum-getvalues-1 + public static T[] GetValues() + where T : struct, Enum => (T[])Enum.GetValues(typeof(T)); + } +} +#endif diff --git a/PolyShim/NetCore10/Enum.cs b/PolyShim/NetCore10/Enum.cs new file mode 100644 index 0000000..f97ac27 --- /dev/null +++ b/PolyShim/NetCore10/Enum.cs @@ -0,0 +1,47 @@ +#if (NETFRAMEWORK && !NET40_OR_GREATER) +#nullable enable +// ReSharper disable RedundantUsingDirective +// ReSharper disable CheckNamespace +// ReSharper disable InconsistentNaming +// ReSharper disable PartialTypeWithSinglePart + +using System; + +internal static partial class PolyfillExtensions +{ + extension(Enum) + { + // https://learn.microsoft.com/dotnet/api/system.enum.tryparse#system-enum-tryparse-1(system-string-system-boolean-0@) + public static bool TryParse(string value, bool ignoreCase, out T result) + where T : struct, Enum + { + try + { + result = (T)Enum.Parse(typeof(T), value, ignoreCase); + return true; + } + catch + { + result = default; + return false; + } + } + + // https://learn.microsoft.com/dotnet/api/system.enum.tryparse#system-enum-tryparse-1(system-string-0@) + public static bool TryParse(string value, out T result) + where T : struct, Enum + { + try + { + result = (T)Enum.Parse(typeof(T), value); + return true; + } + catch + { + result = default; + return false; + } + } + } +} +#endif diff --git a/PolyShim/NetCore20/Enum.cs b/PolyShim/NetCore20/Enum.cs new file mode 100644 index 0000000..b324a75 --- /dev/null +++ b/PolyShim/NetCore20/Enum.cs @@ -0,0 +1,23 @@ +#if (NETCOREAPP && !NETCOREAPP2_0_OR_GREATER) || (NETFRAMEWORK) || (NETSTANDARD && !NETSTANDARD2_1_OR_GREATER) +#nullable enable +// ReSharper disable RedundantUsingDirective +// ReSharper disable CheckNamespace +// ReSharper disable InconsistentNaming +// ReSharper disable PartialTypeWithSinglePart + +using System; + +internal static partial class PolyfillExtensions +{ + extension(Enum) + { + // https://learn.microsoft.com/dotnet/api/system.enum.parse#system-enum-parse-1(system-string-system-boolean) + public static T Parse(string value, bool ignoreCase) + where T : struct, Enum => (T)Enum.Parse(typeof(T), value, ignoreCase); + + // https://learn.microsoft.com/dotnet/api/system.enum.parse#system-enum-parse-1(system-string) + public static T Parse(string value) + where T : struct, Enum => (T)Enum.Parse(typeof(T), value); + } +} +#endif diff --git a/PolyShim/Signatures.md b/PolyShim/Signatures.md index fa02576..6a7fc21 100644 --- a/PolyShim/Signatures.md +++ b/PolyShim/Signatures.md @@ -1,8 +1,8 @@ # Signatures -- **Total:** 238 +- **Total:** 244 - **Types:** 62 -- **Members:** 176 +- **Members:** 182 ___ @@ -42,6 +42,13 @@ ___ - [**[class]**](https://learn.microsoft.com/dotnet/api/system.diagnostics.codeanalysis.dynamicallyaccessedmembersattribute) .NET 5.0 - `DynamicallyAccessedMemberTypes` - [**[enum]**](https://learn.microsoft.com/dotnet/api/system.diagnostics.codeanalysis.dynamicallyaccessedmembertypes) .NET 5.0 +- `Enum` + - [`bool TryParse(string, bool, out T) where T : struct, Enum`](https://learn.microsoft.com/dotnet/api/system.enum.tryparse#system-enum-tryparse-1(system-string-system-boolean-0@)) .NET Core 1.0 + - [`bool TryParse(string, out T) where T : struct, Enum`](https://learn.microsoft.com/dotnet/api/system.enum.tryparse#system-enum-tryparse-1(system-string-0@)) .NET Core 1.0 + - [`string[] GetNames() where T : struct, Enum`](https://learn.microsoft.com/dotnet/api/system.enum.getnames#system-enum-getnames-1) .NET 5.0 + - [`T Parse(string, bool) where T : struct, Enum`](https://learn.microsoft.com/dotnet/api/system.enum.parse#system-enum-parse-1(system-string-system-boolean)) .NET Core 2.0 + - [`T Parse(string) where T : struct, Enum`](https://learn.microsoft.com/dotnet/api/system.enum.parse#system-enum-parse-1(system-string)) .NET Core 2.0 + - [`T[] GetValues() where T : struct, Enum`](https://learn.microsoft.com/dotnet/api/system.enum.getvalues#system-enum-getvalues-1) .NET 5.0 - `Environment` - [`int ProcessId`](https://learn.microsoft.com/dotnet/api/system.environment.processid) .NET 5.0 - [`string? ProcessPath`](https://learn.microsoft.com/dotnet/api/system.environment.processpath) .NET 6.0