Skip to content

Instantly share code, notes, and snippets.

@Sl4vP0weR
Last active May 31, 2024 10:22
Show Gist options
  • Save Sl4vP0weR/5254baf68f11575049fecbeb0d20304c to your computer and use it in GitHub Desktop.
Save Sl4vP0weR/5254baf68f11575049fecbeb0d20304c to your computer and use it in GitHub Desktop.
using System;
using System.Linq;
using System.Collections.Generic;
/// <summary>
/// Enum extensions.
/// </summary>
public static class EnumExtensions
{
/// <inheritdoc cref="EnumExtensions{TEnum}.Values"/>
public static IReadOnlyList<TEnum> GetValues<TEnum>()
where TEnum : Enum => EnumExtensions<TEnum>.Values;
/// <inheritdoc cref="EnumExtensions{TEnum}.Names"/>
public static IReadOnlyList<string> GetNames<TEnum>()
where TEnum : Enum => EnumExtensions<TEnum>.Names;
/// <inheritdoc cref="EnumExtensions{TEnum}.GetFlags(TEnum)"/>
public static IReadOnlyList<TEnum> GetFlags<TEnum>(this TEnum @enum)
where TEnum : Enum => EnumExtensions<TEnum>.GetFlags(@enum);
/// <inheritdoc cref="EnumExtensions{TEnum}.HasFlags(TEnum, TEnum)"/>
public static bool HasFlags<TEnum>(this TEnum @enum, TEnum flags)
where TEnum : Enum => EnumExtensions<TEnum>.HasFlags(@enum, @flags);
/// <inheritdoc cref="EnumExtensions{TEnum}.HasFlags(TEnum, IEnumerable{TEnum})"/>
public static bool HasFlags<TEnum>(this TEnum @enum, IEnumerable<TEnum> flags)
where TEnum : Enum => EnumExtensions<TEnum>.HasFlags(@enum, @flags);
}
/// <summary>
/// <typeparamref name="TEnum"/> extensions.
/// </summary>
public static class EnumExtensions<TEnum>
where TEnum : Enum
{
public static readonly Type Type = typeof (TEnum);
/// <summary>
/// All values of <typeparamref name="TEnum"/>.
/// </summary>
public static readonly IReadOnlyList<TEnum> Values =
Enum.GetValues(Type)
.Cast<TEnum>()
.ToList()
.AsReadOnly();
/// <summary>
/// All names of values of <typeparamref name="TEnum"/>.
/// </summary>
public static readonly IReadOnlyList<string> Names =
Enum.GetNames(Type)
.ToList()
.AsReadOnly();
private static Dictionary<TEnum, IReadOnlyList<TEnum>> flagsCache = new();
/// <summary>
/// Get all flags of <paramref name="enum"/>.
/// </summary>
/// <param name="enum" />
public static IReadOnlyList<TEnum> GetFlags(TEnum @enum)
{
if (!flagsCache.TryGetValue(@enum, out var flags))
flags = Values.Where(value => @enum.HasFlag(value))
.ToList()
.AsReadOnly();
return flags;
}
/// <summary>
/// Determine if <paramref name="enum"/> has all flags.
/// </summary>
/// <param name="enum" />
public static bool HasFlags(TEnum @enum,
TEnum flags) => HasFlags(@enum, GetFlags(flags));
/// <summary>
/// Determine if <paramref name="enum"/> has all flags.
/// </summary>
/// <param name="enum" />
public static bool HasFlags(TEnum @enum,
IEnumerable<TEnum> flags) => flags.All(flag => @enum.HasFlag(flag));
/// <summary>
/// All values of <typeparamref name="TEnum"/> casted to <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T" />
public static IList<T> GetValues<T>() => Values.Cast<T>()
.ToList();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment