PeanutButter
Loading...
Searching...
No Matches
PeanutButter.Utils.FlagExtensions Class Reference

Provides extensions to help with common enum operations. More...

Static Public Member Functions

static bool HasFlag< T > (this T enumValue, T flag)
 Tests if a "flag" enum contains the required flag. Enums which have values declared as powers of 2 can be or'd together to provide a final value, eg when using BindingFlags: var method = typeof(SomeClass).GetMethod("foo", BindingFlags.Public | BindingFlags.Instance);.
static bool HasFlags< T > (this T value, T flag1, T flag2, params T[] moreFlags)
 Tests for multiple flags in an int value.
static bool HasFlags (this int value, int flag1, int flag2, params int[] moreFlags)
 Tests for multiple flags in an int value.
static bool HasFlags (this int value, IEnumerable< int > flags)
 Tests for multiple flags in an int value.
static T WithFlag< T > (this T enumValue, T flag)
 Returns a new value that is the original enum with the required flag added.
static int WithFlag (this int value, int flag)
 Returns a new value that is the original enum with the required flag added.
static T WithoutFlag< T > (this T enumValue, T flag)
 Returns a new value that is the original enum value without the provided flag.
static int WithoutFlag (this int value, int flag)
 Returns a new value which is the original integer value without the provided integer flag.
static bool HasFlag (this int value, int flag)
 Provides the integer analogy to testing flags in an enum value, since the former relies on integer math anyway. This essentially tests if the flag's on-bits are all on in the value, eg 6.HasFlag(2); // true.

Detailed Description

Provides extensions to help with common enum operations.

Member Function Documentation

◆ HasFlag()

bool PeanutButter.Utils.FlagExtensions.HasFlag ( this int value,
int flag )
static

Provides the integer analogy to testing flags in an enum value, since the former relies on integer math anyway. This essentially tests if the flag's on-bits are all on in the value, eg 6.HasFlag(2); // true.

Parameters
value
flag
Returns

◆ HasFlag< T >()

bool PeanutButter.Utils.FlagExtensions.HasFlag< T > ( this T enumValue,
T flag )
static

Tests if a "flag" enum contains the required flag. Enums which have values declared as powers of 2 can be or'd together to provide a final value, eg when using BindingFlags: var method = typeof(SomeClass).GetMethod("foo", BindingFlags.Public | BindingFlags.Instance);.

  • in this case, one could do something like: if (flags.HasFlag(BindingFlags.Public)) { // do whatever one does with public members }
Parameters
enumValue
flag
Template Parameters
T
Returns
Type Constraints
T :struct 

◆ HasFlags() [1/2]

bool PeanutButter.Utils.FlagExtensions.HasFlags ( this int value,
IEnumerable< int > flags )
static

Tests for multiple flags in an int value.

Parameters
value
flags
Returns

◆ HasFlags() [2/2]

bool PeanutButter.Utils.FlagExtensions.HasFlags ( this int value,
int flag1,
int flag2,
params int[] moreFlags )
static

Tests for multiple flags in an int value.

Parameters
value
flag1
flag2
moreFlags
Returns

◆ HasFlags< T >()

bool PeanutButter.Utils.FlagExtensions.HasFlags< T > ( this T value,
T flag1,
T flag2,
params T[] moreFlags )
static

Tests for multiple flags in an int value.

Parameters
value
flag1
flag2
moreFlags
Template Parameters
T
Returns

◆ WithFlag()

int PeanutButter.Utils.FlagExtensions.WithFlag ( this int value,
int flag )
static

Returns a new value that is the original enum with the required flag added.

Parameters
value
flag
Returns

◆ WithFlag< T >()

T PeanutButter.Utils.FlagExtensions.WithFlag< T > ( this T enumValue,
T flag )
static

Returns a new value that is the original enum with the required flag added.

Parameters
enumValue
flag
Template Parameters
T
Returns
Type Constraints
T :struct 

◆ WithoutFlag()

int PeanutButter.Utils.FlagExtensions.WithoutFlag ( this int value,
int flag )
static

Returns a new value which is the original integer value without the provided integer flag.

Parameters
value
flag
Returns

◆ WithoutFlag< T >()

T PeanutButter.Utils.FlagExtensions.WithoutFlag< T > ( this T enumValue,
T flag )
static

Returns a new value that is the original enum value without the provided flag.

Parameters
enumValue
flag
Template Parameters
T
Returns
Type Constraints
T :struct 

The documentation for this class was generated from the following file:
  • source/Utils/PeanutButter.Utils/FlagExtensions.cs