EnumUtils correctly handling boxed enum values

This commit is contained in:
Chris Cameron
2017-07-05 16:53:09 -04:00
parent d0295fc926
commit 705ecc0af9
2 changed files with 52 additions and 38 deletions

View File

@@ -21,23 +21,34 @@ namespace ICD.Common.Utils
/// Returns true if the given type is an enum. /// Returns true if the given type is an enum.
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public static bool IsEnum(Type type) public static bool IsEnumType(Type type)
{ {
return type == typeof(Enum) || type if (type == null)
throw new ArgumentNullException("type");
return type.IsAssignableTo(typeof(Enum)) || type
#if !SIMPLSHARP #if !SIMPLSHARP
.GetTypeInfo() .GetTypeInfo()
#endif #endif
.IsEnum; .IsEnum;
} }
/// <summary> /// <summary>
/// Returns true if the given type is an enum. /// Returns true if the given type is an enum.
/// </summary> /// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns> /// <returns></returns>
public static bool IsEnum<T>() public static bool IsEnumType<T>()
{ {
return IsEnum(typeof(T)); return IsEnumType(typeof(T));
}
/// <summary>
/// Returns true if the given value is an enum.
/// </summary>
/// <returns></returns>
public static bool IsEnum(object value)
{
return value != null && IsEnumType(value.GetType());
} }
#region Values #region Values
@@ -49,7 +60,7 @@ namespace ICD.Common.Utils
/// <returns></returns> /// <returns></returns>
public static object GetUnderlyingValue<T>(T value) public static object GetUnderlyingValue<T>(T value)
{ {
if (!IsEnum(typeof(T))) if (!IsEnumType(typeof(T)))
throw new InvalidOperationException(string.Format("{0} is not an enum", value.GetType().Name)); throw new InvalidOperationException(string.Format("{0} is not an enum", value.GetType().Name));
#if SIMPLSHARP #if SIMPLSHARP
@@ -96,7 +107,7 @@ namespace ICD.Common.Utils
if (type == null) if (type == null)
throw new ArgumentNullException("type"); throw new ArgumentNullException("type");
if (!IsEnum(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
@@ -116,7 +127,7 @@ namespace ICD.Common.Utils
/// <returns></returns> /// <returns></returns>
public static T GetNoneValue<T>() public static T GetNoneValue<T>()
{ {
if (!IsEnum(typeof(T))) if (!IsEnumType(typeof(T)))
throw new InvalidOperationException(string.Format("{0} is not an enum", typeof(T).Name)); throw new InvalidOperationException(string.Format("{0} is not an enum", typeof(T).Name));
return (T)(object)0; return (T)(object)0;
@@ -129,7 +140,7 @@ namespace ICD.Common.Utils
/// <returns></returns> /// <returns></returns>
public static IEnumerable<T> GetValuesExceptNone<T>() public static IEnumerable<T> GetValuesExceptNone<T>()
{ {
if (!IsEnum(typeof(T))) if (!IsEnumType(typeof(T)))
throw new InvalidOperationException(string.Format("{0} is not an enum", typeof(T).Name)); throw new InvalidOperationException(string.Format("{0} is not an enum", typeof(T).Name));
return GetValuesExceptNone(typeof(T)).Cast<T>(); return GetValuesExceptNone(typeof(T)).Cast<T>();
@@ -145,7 +156,7 @@ namespace ICD.Common.Utils
if (type == null) if (type == null)
throw new ArgumentNullException("type"); throw new ArgumentNullException("type");
if (!IsEnum(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 GetValues(type).Where(v => (int)v != 0); return GetValues(type).Where(v => (int)v != 0);
@@ -162,7 +173,7 @@ namespace ICD.Common.Utils
/// <returns></returns> /// <returns></returns>
public static bool IsFlagsEnum<T>() public static bool IsFlagsEnum<T>()
{ {
if (!IsEnum<T>()) if (!IsEnumType<T>())
throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name)); throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name));
return IsFlagsEnum(typeof(T)); return IsFlagsEnum(typeof(T));
@@ -177,7 +188,7 @@ namespace ICD.Common.Utils
if (type == null) if (type == null)
throw new ArgumentNullException("type"); throw new ArgumentNullException("type");
if (!IsEnum(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
@@ -195,9 +206,6 @@ namespace ICD.Common.Utils
/// <returns></returns> /// <returns></returns>
public static T GetFlagsIntersection<T>(params T[] values) public static T GetFlagsIntersection<T>(params T[] values)
{ {
if (!IsEnum<T>())
throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name));
if (values.Length == 0) if (values.Length == 0)
return GetNoneValue<T>(); return GetNoneValue<T>();
@@ -216,8 +224,8 @@ namespace ICD.Common.Utils
/// <returns></returns> /// <returns></returns>
public static IEnumerable<T> GetFlags<T>(T value) public static IEnumerable<T> GetFlags<T>(T value)
{ {
if (!IsEnum<T>()) if (!IsEnum(value))
throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name)); throw new ArgumentException(string.Format("{0} is not an enum", value == null ? "NULL" : value.GetType().Name), "value");
return GetValues<T>().Where(e => HasFlag(value, e)); return GetValues<T>().Where(e => HasFlag(value, e));
} }
@@ -230,8 +238,8 @@ namespace ICD.Common.Utils
/// <returns></returns> /// <returns></returns>
public static IEnumerable<T> GetFlagsExceptNone<T>(T value) public static IEnumerable<T> GetFlagsExceptNone<T>(T value)
{ {
if (!IsEnum<T>()) if (!IsEnum(value))
throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name)); throw new ArgumentException(string.Format("{0} is not an enum", value == null ? "NULL" : value.GetType().Name), "value");
T none = GetNoneValue<T>(); T none = GetNoneValue<T>();
return GetFlags(value).Except(none); return GetFlags(value).Except(none);
@@ -244,7 +252,7 @@ namespace ICD.Common.Utils
/// <returns></returns> /// <returns></returns>
public static T GetFlagsAllValue<T>() public static T GetFlagsAllValue<T>()
{ {
if (!IsEnum<T>()) if (!IsEnumType<T>())
throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name)); throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name));
int output = GetValues<T>().Aggregate(0, (current, value) => current | (int)(object)value); int output = GetValues<T>().Aggregate(0, (current, value) => current | (int)(object)value);
@@ -260,8 +268,11 @@ namespace ICD.Common.Utils
/// <returns></returns> /// <returns></returns>
public static bool HasFlag<T>(T value, T flag) public static bool HasFlag<T>(T value, T flag)
{ {
if (!IsEnum<T>()) if (!IsEnum(value))
throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name)); throw new ArgumentException(string.Format("{0} is not an enum", value == null ? "NULL" : value.GetType().Name), "value");
if (!IsEnum(flag))
throw new ArgumentException(string.Format("{0} is not an enum", flag == null ? "NULL" : flag.GetType().Name), "flag");
return ToEnum(value).HasFlag(ToEnum(flag)); return ToEnum(value).HasFlag(ToEnum(flag));
} }
@@ -275,8 +286,11 @@ namespace ICD.Common.Utils
/// <returns></returns> /// <returns></returns>
public static bool HasFlags<T>(T value, T flags) public static bool HasFlags<T>(T value, T flags)
{ {
if (!IsEnum<T>()) if (!IsEnum(value))
throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name)); throw new ArgumentException(string.Format("{0} is not an enum", value == null ? "NULL" : value.GetType().Name), "value");
if (!IsEnum(flags))
throw new ArgumentException(string.Format("{0} is not an enum", flags == null ? "NULL" : flags.GetType().Name), "flags");
return ToEnum(value).HasFlags(ToEnum(flags)); return ToEnum(value).HasFlags(ToEnum(flags));
} }
@@ -289,8 +303,8 @@ namespace ICD.Common.Utils
/// <returns></returns> /// <returns></returns>
public static bool HasSingleFlag<T>(T value) public static bool HasSingleFlag<T>(T value)
{ {
if (!IsEnum<T>()) if (!IsEnum(value))
throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name)); throw new ArgumentException(string.Format("{0} is not an enum", value == null ? "NULL" : value.GetType().Name), "value");
return (int)(object)value != (int)(object)GetNoneValue<T>() && !HasMultipleFlags(value); return (int)(object)value != (int)(object)GetNoneValue<T>() && !HasMultipleFlags(value);
} }
@@ -302,8 +316,8 @@ namespace ICD.Common.Utils
/// <returns></returns> /// <returns></returns>
public static bool HasMultipleFlags<T>(T value) public static bool HasMultipleFlags<T>(T value)
{ {
if (!IsEnum<T>()) if (!IsEnum(value))
throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name)); throw new ArgumentException(string.Format("{0} is not an enum", value == null ? "NULL" : value.GetType().Name), "value");
return HasMultipleFlags((int)(object)value); return HasMultipleFlags((int)(object)value);
} }
@@ -331,7 +345,7 @@ namespace ICD.Common.Utils
/// <returns></returns> /// <returns></returns>
public static T Parse<T>(string data, bool ignoreCase) public static T Parse<T>(string data, bool ignoreCase)
{ {
if (!IsEnum<T>()) if (!IsEnumType<T>())
throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name)); throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name));
try try
@@ -355,7 +369,7 @@ namespace ICD.Common.Utils
/// <returns></returns> /// <returns></returns>
public static bool TryParse<T>(string data, bool ignoreCase, out T result) public static bool TryParse<T>(string data, bool ignoreCase, out T result)
{ {
if (!IsEnum<T>()) if (!IsEnumType<T>())
throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name)); throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name));
result = default(T); result = default(T);
@@ -379,8 +393,8 @@ namespace ICD.Common.Utils
/// <returns></returns> /// <returns></returns>
public static Enum ToEnum<T>(T value) public static Enum ToEnum<T>(T value)
{ {
if (!IsEnum<T>()) if (!IsEnum(value))
throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name)); throw new ArgumentException(string.Format("{0} is not an enum", value == null ? "NULL" : value.GetType().Name), "value");
return ToEnum((object)value); return ToEnum((object)value);
} }

View File

@@ -435,7 +435,7 @@ namespace ICD.Common.Utils.Xml
[PublicAPI] [PublicAPI]
public static T ReadChildElementContentAsEnum<T>(string xml, string childElement, bool ignoreCase) public static T ReadChildElementContentAsEnum<T>(string xml, string childElement, bool ignoreCase)
{ {
if (!EnumUtils.IsEnum<T>()) if (!EnumUtils.IsEnumType<T>())
throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name)); throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name));
string child = GetChildElementAsString(xml, childElement); string child = GetChildElementAsString(xml, childElement);
@@ -595,7 +595,7 @@ namespace ICD.Common.Utils.Xml
public static T? TryReadChildElementContentAsEnum<T>(string xml, string childElement, bool ignoreCase) public static T? TryReadChildElementContentAsEnum<T>(string xml, string childElement, bool ignoreCase)
where T : struct where T : struct
{ {
if (!EnumUtils.IsEnum<T>()) if (!EnumUtils.IsEnumType<T>())
throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name)); throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name));
T output; T output;
@@ -614,7 +614,7 @@ namespace ICD.Common.Utils.Xml
[PublicAPI] [PublicAPI]
public static bool TryReadChildElementContentAsEnum<T>(string xml, string childElement, bool ignoreCase, out T output) public static bool TryReadChildElementContentAsEnum<T>(string xml, string childElement, bool ignoreCase, out T output)
{ {
if (!EnumUtils.IsEnum<T>()) if (!EnumUtils.IsEnumType<T>())
throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name)); throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name));
output = default(T); output = default(T);
@@ -709,7 +709,7 @@ namespace ICD.Common.Utils.Xml
[PublicAPI] [PublicAPI]
public static T ReadElementContentAsEnum<T>(string xml, bool ignoreCase) public static T ReadElementContentAsEnum<T>(string xml, bool ignoreCase)
{ {
if (!EnumUtils.IsEnum<T>()) if (!EnumUtils.IsEnumType<T>())
throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name)); throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name));
using (IcdXmlReader reader = new IcdXmlReader(xml)) using (IcdXmlReader reader = new IcdXmlReader(xml))