Skip to content

Commit 8ce57ad

Browse files
authored
Add polyfills for Enum.IsDefined<T>(...) and Enum.HasFlag(...) (#57)
1 parent 51eae5d commit 8ce57ad

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

PolyShim.Tests/Net50/EnumTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ namespace PolyShim.Tests.Net50;
66

77
public class EnumTests
88
{
9+
[Fact]
10+
public void IsDefined_Test()
11+
{
12+
// Act & assert
13+
Enum.IsDefined((DayOfWeek)0).Should().BeTrue();
14+
Enum.IsDefined((DayOfWeek)3).Should().BeTrue();
15+
Enum.IsDefined((DayOfWeek)7).Should().BeFalse();
16+
}
17+
918
[Fact]
1019
public void GetNames_Test()
1120
{

PolyShim/Net50/Enum.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ internal static partial class PolyfillExtensions
1111
{
1212
extension(Enum)
1313
{
14+
// https://learn.microsoft.com/dotnet/api/system.enum.isdefined#system-enum-isdefined-1(-0)
15+
public static bool IsDefined<T>(T value)
16+
where T : struct, Enum => Enum.IsDefined(typeof(T), value);
17+
1418
// https://learn.microsoft.com/dotnet/api/system.enum.getnames#system-enum-getnames-1
1519
public static string[] GetNames<T>()
1620
where T : struct, Enum => Enum.GetNames(typeof(T));

PolyShim/NetCore10/Enum.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@
99

1010
internal static partial class PolyfillExtensions
1111
{
12+
extension(Enum enumValue)
13+
{
14+
// https://learn.microsoft.com/dotnet/api/system.enum.hasflag
15+
public bool HasFlag(Enum flag)
16+
{
17+
var thisValue = Convert.ToInt64(enumValue);
18+
var flagValue = Convert.ToInt64(flag);
19+
20+
return (thisValue & flagValue) == flagValue;
21+
}
22+
}
23+
1224
extension(Enum)
1325
{
1426
// https://learn.microsoft.com/dotnet/api/system.enum.tryparse#system-enum-tryparse-1(system-string-system-boolean-0@)

PolyShim/Signatures.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Signatures
22

3-
- **Total:** 244
3+
- **Total:** 246
44
- **Types:** 62
5-
- **Members:** 182
5+
- **Members:** 184
66

77
___
88

@@ -43,6 +43,8 @@ ___
4343
- `DynamicallyAccessedMemberTypes`
4444
- [**[enum]**](https://learn.microsoft.com/dotnet/api/system.diagnostics.codeanalysis.dynamicallyaccessedmembertypes) <sup><sub>.NET 5.0</sub></sup>
4545
- `Enum`
46+
- [`bool HasFlag(Enum)`](https://learn.microsoft.com/dotnet/api/system.enum.hasflag) <sup><sub>.NET Core 1.0</sub></sup>
47+
- [`bool IsDefined<T>(T) where T : struct, Enum`](https://learn.microsoft.com/dotnet/api/system.enum.isdefined#system-enum-isdefined-1(-0)) <sup><sub>.NET 5.0</sub></sup>
4648
- [`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>
4749
- [`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>
4850
- [`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>

0 commit comments

Comments
 (0)