Skip to content
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
34 changes: 34 additions & 0 deletions PolyShim.Tests/Net50/EnumTests.cs
Original file line number Diff line number Diff line change
@@ -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<DayOfWeek>()
.Should()
.Equal("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
}

[Fact]
public void GetValues_Test()
{
// Act & assert
Enum.GetValues<DayOfWeek>()
.Should()
.Equal(
DayOfWeek.Sunday,
DayOfWeek.Monday,
DayOfWeek.Tuesday,
DayOfWeek.Wednesday,
DayOfWeek.Thursday,
DayOfWeek.Friday,
DayOfWeek.Saturday
);
}
}
18 changes: 18 additions & 0 deletions PolyShim.Tests/NetCore20/EnumTests.cs
Original file line number Diff line number Diff line change
@@ -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<DayOfWeek>("Friday").Should().Be(DayOfWeek.Friday);
Enum.Parse<DayOfWeek>("friday", true).Should().Be(DayOfWeek.Friday);
Assert.Throws<ArgumentException>(() => Enum.Parse<DayOfWeek>("Moonday"));
Assert.Throws<ArgumentException>(() => Enum.Parse<DayOfWeek>("friday", false));
}
}
23 changes: 23 additions & 0 deletions PolyShim/Net50/Enum.cs
Original file line number Diff line number Diff line change
@@ -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<T>()
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<T>()
where T : struct, Enum => (T[])Enum.GetValues(typeof(T));
}
}
#endif
47 changes: 47 additions & 0 deletions PolyShim/NetCore10/Enum.cs
Original file line number Diff line number Diff line change
@@ -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<T>(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<T>(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
23 changes: 23 additions & 0 deletions PolyShim/NetCore20/Enum.cs
Original file line number Diff line number Diff line change
@@ -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<T>(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<T>(string value)
where T : struct, Enum => (T)Enum.Parse(typeof(T), value);
}
}
#endif
11 changes: 9 additions & 2 deletions PolyShim/Signatures.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Signatures

- **Total:** 238
- **Total:** 244
- **Types:** 62
- **Members:** 176
- **Members:** 182

___

Expand Down Expand Up @@ -42,6 +42,13 @@ ___
- [**[class]**](https://learn.microsoft.com/dotnet/api/system.diagnostics.codeanalysis.dynamicallyaccessedmembersattribute) <sup><sub>.NET 5.0</sub></sup>
- `DynamicallyAccessedMemberTypes`
- [**[enum]**](https://learn.microsoft.com/dotnet/api/system.diagnostics.codeanalysis.dynamicallyaccessedmembertypes) <sup><sub>.NET 5.0</sub></sup>
- `Enum`
- [`bool TryParse<T>(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@)) <sup><sub>.NET Core 1.0</sub></sup>
- [`bool TryParse<T>(string, out T) where T : struct, Enum`](https://learn.microsoft.com/dotnet/api/system.enum.tryparse#system-enum-tryparse-1(system-string-0@)) <sup><sub>.NET Core 1.0</sub></sup>
- [`string[] GetNames<T>() where T : struct, Enum`](https://learn.microsoft.com/dotnet/api/system.enum.getnames#system-enum-getnames-1) <sup><sub>.NET 5.0</sub></sup>
- [`T Parse<T>(string, bool) where T : struct, Enum`](https://learn.microsoft.com/dotnet/api/system.enum.parse#system-enum-parse-1(system-string-system-boolean)) <sup><sub>.NET Core 2.0</sub></sup>
- [`T Parse<T>(string) where T : struct, Enum`](https://learn.microsoft.com/dotnet/api/system.enum.parse#system-enum-parse-1(system-string)) <sup><sub>.NET Core 2.0</sub></sup>
- [`T[] GetValues<T>() where T : struct, Enum`](https://learn.microsoft.com/dotnet/api/system.enum.getvalues#system-enum-getvalues-1) <sup><sub>.NET 5.0</sub></sup>
- `Environment`
- [`int ProcessId`](https://learn.microsoft.com/dotnet/api/system.environment.processid) <sup><sub>.NET 5.0</sub></sup>
- [`string? ProcessPath`](https://learn.microsoft.com/dotnet/api/system.environment.processpath) <sup><sub>.NET 6.0</sub></sup>
Expand Down
Loading