Skip to content

Commit

Permalink
Add enum extensions and enum flag extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
nh43de committed Mar 25, 2024
1 parent 009ae90 commit 124a338
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/DataPowerTools/Extensions/EnumExtensions.cs
@@ -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));
//}
}
}
87 changes: 87 additions & 0 deletions src/DataPowerTools/Extensions/EnumFlagExtensions.cs
@@ -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);
}
}
}
}

0 comments on commit 124a338

Please sign in to comment.