feat: Adding missing enum utils methods for metlife

This commit is contained in:
Chris Cameron
2018-07-26 09:56:36 -04:00
parent 3129d3e60c
commit 1a87ce9f00

View File

@@ -108,15 +108,31 @@ namespace ICD.Common.Utils
return cache as T[]; return cache as T[];
} }
/// <summary>
/// Gets the values from an enumeration without performing any caching. This is slow because of reflection.
/// </summary>
/// <returns></returns>
public static IEnumerable<int> GetValues(Type type)
{
return GetValuesUncached(type).Cast<int>();
}
/// <summary> /// <summary>
/// Gets the values from an enumeration without performing any caching. This is slow because of reflection. /// Gets the values from an enumeration without performing any caching. This is slow because of reflection.
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
private static IEnumerable<T> GetValuesUncached<T>() private static IEnumerable<T> GetValuesUncached<T>()
{ {
Type type = typeof(T); return GetValuesUncached(typeof(T)).Cast<T>();
}
if (!IsEnumType<T>()) /// <summary>
/// Gets the values from an enumeration without performing any caching. This is slow because of reflection.
/// </summary>
/// <returns></returns>
private static IEnumerable<object> GetValuesUncached(Type type)
{
if (!IsEnumType(type))
throw new InvalidOperationException(string.Format("{0} is not an enum", type.Name)); throw new InvalidOperationException(string.Format("{0} is not an enum", type.Name));
return type return type
@@ -126,8 +142,7 @@ namespace ICD.Common.Utils
.GetTypeInfo() .GetTypeInfo()
#endif #endif
.GetFields(BindingFlags.Static | BindingFlags.Public) .GetFields(BindingFlags.Static | BindingFlags.Public)
.Select(x => x.GetValue(null)) .Select(x => x.GetValue(null));
.Cast<T>();
} }
/// <summary> /// <summary>
@@ -140,6 +155,15 @@ namespace ICD.Common.Utils
return GetFlagsExceptNone<T>(); return GetFlagsExceptNone<T>();
} }
/// <summary>
/// Gets the values from an enumeration except the 0 value without performing any caching. This is slow because of reflection.
/// </summary>
/// <returns></returns>
public static IEnumerable<int> GetValuesExceptNone(Type type)
{
return GetValues(type).Except(0);
}
#endregion #endregion
#region Flags #region Flags
@@ -151,10 +175,16 @@ namespace ICD.Common.Utils
/// <returns></returns> /// <returns></returns>
public static bool IsFlagsEnum<T>() public static bool IsFlagsEnum<T>()
{ {
if (!IsEnumType<T>()) return IsFlagsEnum(typeof(T));
throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name)); }
return typeof(T) /// <summary>
/// Returns true if the given enum type has the Flags attribute set.
/// </summary>
/// <returns></returns>
public static bool IsFlagsEnum(Type type)
{
return type
#if !SIMPLSHARP #if !SIMPLSHARP
.GetTypeInfo() .GetTypeInfo()
#endif #endif