Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add enum extensions and enum flag extensions
- Loading branch information
Showing
2 changed files
with
112 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System; | ||
|
||
namespace DataPowerTools.Extensions | ||
{ | ||
/// <summary> | ||
/// Provides useful extensions for enumerations. | ||
/// </summary> | ||
public static class EnumExtensions | ||
{ | ||
public static T ParseEnumType<T>(this string enumString) | ||
{ | ||
return (T) Enum.Parse(typeof(T), enumString); | ||
} | ||
|
||
//public static string WriteEnumAsString<TEnum>(TEnum tEnum) where TEnum : Enum | ||
//{ | ||
// return tEnum.ToString(); | ||
//} | ||
|
||
//public static string[] GetEnumOptionsAsStringArray<TEnum>(this TEnum t) where TEnum : Enum | ||
//{ | ||
// return Enum.GetNames(typeof(TEnum)); | ||
//} | ||
} | ||
} |
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,87 @@ | ||
using System; | ||
|
||
namespace DataPowerTools.Extensions | ||
{ | ||
/// <summary> | ||
/// Provides useful extensions for enumerations. | ||
/// </summary> | ||
public static class EnumFlagExtensions | ||
{ | ||
/// <summary> | ||
/// Determines if a bit flag or set of bit flags are set in an enumeration value. | ||
/// </summary> | ||
/// <typeparam name="T">The type of enumeration.</typeparam> | ||
/// <param name="value">The value to test for the bit flag(s).</param> | ||
/// <param name="flag">The flag(s) to test for.</param> | ||
/// <returns><c>true</c> if <paramref name="value"/> contains the bit flags defined in <paramref name="flag"/>; otherwise, <c>false</c>.</returns> | ||
public static bool Contains<T>(this Enum value, T flag) | ||
{ | ||
try | ||
{ | ||
return (Convert.ToInt64(value) & Convert.ToInt64(flag)) != 0; | ||
} | ||
catch (OverflowException) | ||
{ | ||
return (Convert.ToUInt64(value) & Convert.ToUInt64(flag)) != 0; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Combines an enumeration value with a bit flag or set of bit flags and returns the new value. | ||
/// </summary> | ||
/// <typeparam name="T">The type of enumeration.</typeparam> | ||
/// <param name="value">The value to combine with the bit flag(s).</param> | ||
/// <param name="flag">The flag(s) to add to <paramref name="value"/>.</param> | ||
/// <returns><paramref name="value"/> combined with <paramref name="flag"/>.</returns> | ||
public static T Add<T>(this Enum value, T flag) | ||
{ | ||
try | ||
{ | ||
return (T)Convert.ChangeType(Convert.ToInt64(value) | Convert.ToInt64(flag), Enum.GetUnderlyingType(typeof(T))); | ||
} | ||
catch (OverflowException) | ||
{ | ||
return (T)Convert.ChangeType(Convert.ToUInt64(value) | Convert.ToUInt64(flag), Enum.GetUnderlyingType(typeof(T))); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Removes a bit flag of set of bit flags from an enumeration value and returns the new value. | ||
/// </summary> | ||
/// <typeparam name="T">The type of enumeration.</typeparam> | ||
/// <param name="value">The value from which to remove the bit flag(s).</param> | ||
/// <param name="flag">The flag(s) to remove from <paramref name="value"/>.</param> | ||
/// <returns><paramref name="value"/> with any flags in <paramref name="flag"/> removed.</returns> | ||
public static T Remove<T>(this Enum value, T flag) | ||
{ | ||
try | ||
{ | ||
return (T)Convert.ChangeType(Convert.ToInt64(value) & ~Convert.ToInt64(flag), Enum.GetUnderlyingType(typeof(T))); | ||
} | ||
catch (OverflowException) | ||
{ | ||
return (T)Convert.ChangeType(Convert.ToUInt64(value) & ~Convert.ToUInt64(flag), Enum.GetUnderlyingType(typeof(T))); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Adds or removes a bit flag or set of bit flags in an enumeration value and returns the new value. | ||
/// </summary> | ||
/// <typeparam name="T">The type of enumeration.</typeparam> | ||
/// <param name="value">The value on which to operate.</param> | ||
/// <param name="flag">The flag(s) to add to or remove from <paramref name="value"/>.</param> | ||
/// <param name="add">Whether to add or remove the flags; <c>true</c> adds the flags; <c>false</c> removes them.</param> | ||
/// <returns>The new value.</returns> | ||
public static T AddOrRemove<T>(this Enum value, T flag, bool add) | ||
{ | ||
if (add) | ||
{ | ||
return value.Add(flag); | ||
} | ||
else | ||
{ | ||
return value.Remove(flag); | ||
} | ||
} | ||
} | ||
} |