using Cobilas.Collections;
using System.Collections.Generic;
namespace System {
#pragma warning disable CS1591 // O comentário XML ausente não foi encontrado para o tipo ou membro visível publicamente
public static class Enum_CB_Extension {
#pragma warning restore CS1591 // O comentário XML ausente não foi encontrado para o tipo ou membro visível publicamente
///
/// Determines whether one or more bit fields are set in the current instance.
///
/// The target enumerator.
/// Comparison tags.
public static bool HasFlag(this Enum E, params Enum[] flags) {
for (int I = 0; I < ArrayManipulation.ArrayLength(flags); I++)
if (E.HasFlag(flags[I]))
return true;
return false;
}
///
/// Gets the tag with the current assigned value.
///
/// The target enumerator.
public static KeyValuePair GetEnumPair(this Enum E) {
KeyValuePair[] pairs = GetEnumPairs(E);
if (E.ToString().Contains(',')) {
string[] flags = E.ToString().Split(',');
int res = 0;
for (int A = 0; A < flags.Length; A++)
for (int B = 0; B < pairs.Length; B++)
if (pairs[B].Key == flags[A].Trim())
res |= pairs[B].Value;
return new KeyValuePair(E.ToString(), res);
} else {
for (int I = 0; I < ArrayManipulation.ArrayLength(pairs); I++)
if (pairs[I].Key == Enum.GetName(E.GetType(), E))
return pairs[I];
}
return default;
}
///
/// Gets a list of all tags along with their assigned values.
///
/// The target enumerator.
/// enumType is null.
/// Returns a list of all tags along with their assigned values or an empty list.
public static KeyValuePair[] GetEnumPairs(this Enum E) {
Array array = Enum.GetValues(E.GetType());
KeyValuePair[] Res = Array.Empty>();
for (int I = 0; I < ArrayManipulation.ArrayLength(array); I++)
ArrayManipulation.Add(new KeyValuePair(
array.GetValue(I).ToString(),
(int)array.GetValue(I)
), ref Res);
return Res;
}
///
/// Retrieves the name of the constant in the specified enumeration that has the specified value.
///
/// The target enumerator.
/// The value of a particular enumerated constant in terms of its underlying type.
/// enumType is null.
/// enumType is not an . -or- value is neither of type enumType nor does
/// it have the same underlying type as enumType.
///
/// A containing the name of the enumerated constant in enumType whose value is value; or null if no such constant is found.
public static string GetName(this Enum E, object value)
=> Enum.GetName(E.GetType(), value);
///
/// Retrieves the name of the constant in the specified enumeration that has the specified value.
///
/// The target enumerator.
/// enumType is null.
/// enumType is not an . -or- value is neither of type enumType nor does
/// it have the same underlying type as enumType.
///
/// A string containing the name of the enumerated constant in enumType whose value is value; or null if no such constant is found.
public static string GetName(this Enum E)
=> GetName(E, E);
///
/// Retrieves an array of the names of the constants in a specified enumeration.
///
/// The target enumerator.
/// enumType is null.
/// enumType parameter is not an .
/// A string array of the names of the constants in enumType.
public static string[] GetNames(this Enum E)
=> Enum.GetNames(E.GetType());
///
/// Converts the specified value of a specified enumerated type to its equivalent representation according to the specified format.
///
/// The target enumerator.
/// The value to convert.
/// The output format to use.
/// The enumType, value, or format parameter is null.
/// The enumType parameter is not an type. -or- The value is from an
/// enumeration that differs in type from enumType. -or- The type of value is not
/// an underlying type of enumType.
///
/// The format parameter contains an invalid value.
/// format equals "X", but the enumeration type is unknown.
/// A string representation of value.
public static string Format(this Enum E, object value, string format)
=> Enum.Format(E.GetType(), value, format);
}
}