-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds Flags support vi Enum.IsDefinedWithFlagsSupport
Adds support for flags when verifying enums.
- Loading branch information
Showing
6 changed files
with
223 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
namespace EnsureThat.Internals | ||
{ | ||
internal static class EnumOf<T> where T : struct, Enum | ||
{ | ||
internal static readonly Type EnumType; | ||
private static readonly bool _hasFlags; | ||
private static readonly List<ulong> _values; | ||
|
||
static EnumOf() | ||
{ | ||
EnumType = typeof(T); | ||
|
||
_hasFlags = EnumType.GetTypeInfo().GetCustomAttributes<FlagsAttribute>(false).Any(); | ||
|
||
var enumValues = Enum.GetValues(EnumType); | ||
_values = new List<ulong>(enumValues.Length); | ||
foreach (var v in enumValues) | ||
_values.Add(Convert.ToUInt64(v)); | ||
} | ||
|
||
internal static bool Contains(T value) | ||
{ | ||
if (!_hasFlags) | ||
return Enum.IsDefined(EnumType, value); | ||
|
||
var raw = Convert.ToUInt64(value); | ||
if (raw == 0) | ||
return Enum.IsDefined(EnumType, value); | ||
|
||
ulong sum = 0; | ||
|
||
foreach (var val in _values) | ||
{ | ||
if ((raw & val) == val) | ||
sum |= val; | ||
} | ||
|
||
return sum == raw; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters