diff --git a/ICD.Common.Utils/Utils/EnumUtils.cs b/ICD.Common.Utils/Utils/EnumUtils.cs index 4d540ee..4591857 100644 --- a/ICD.Common.Utils/Utils/EnumUtils.cs +++ b/ICD.Common.Utils/Utils/EnumUtils.cs @@ -21,23 +21,34 @@ namespace ICD.Common.Utils /// Returns true if the given type is an enum. /// /// - 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 - .GetTypeInfo() + .GetTypeInfo() #endif - .IsEnum; + .IsEnum; } /// /// Returns true if the given type is an enum. /// - /// /// - public static bool IsEnum() + public static bool IsEnumType() { - return IsEnum(typeof(T)); + return IsEnumType(typeof(T)); + } + + /// + /// Returns true if the given value is an enum. + /// + /// + public static bool IsEnum(object value) + { + return value != null && IsEnumType(value.GetType()); } #region Values @@ -49,7 +60,7 @@ namespace ICD.Common.Utils /// public static object GetUnderlyingValue(T value) { - if (!IsEnum(typeof(T))) + if (!IsEnumType(typeof(T))) throw new InvalidOperationException(string.Format("{0} is not an enum", value.GetType().Name)); #if SIMPLSHARP @@ -96,7 +107,7 @@ namespace ICD.Common.Utils if (type == null) throw new ArgumentNullException("type"); - if (!IsEnum(type)) + if (!IsEnumType(type)) throw new InvalidOperationException(string.Format("{0} is not an enum", type.Name)); return type @@ -116,7 +127,7 @@ namespace ICD.Common.Utils /// public static T GetNoneValue() { - if (!IsEnum(typeof(T))) + if (!IsEnumType(typeof(T))) throw new InvalidOperationException(string.Format("{0} is not an enum", typeof(T).Name)); return (T)(object)0; @@ -129,7 +140,7 @@ namespace ICD.Common.Utils /// public static IEnumerable GetValuesExceptNone() { - if (!IsEnum(typeof(T))) + if (!IsEnumType(typeof(T))) throw new InvalidOperationException(string.Format("{0} is not an enum", typeof(T).Name)); return GetValuesExceptNone(typeof(T)).Cast(); @@ -145,7 +156,7 @@ namespace ICD.Common.Utils if (type == null) throw new ArgumentNullException("type"); - if (!IsEnum(type)) + if (!IsEnumType(type)) throw new InvalidOperationException(string.Format("{0} is not an enum", type.Name)); return GetValues(type).Where(v => (int)v != 0); @@ -162,7 +173,7 @@ namespace ICD.Common.Utils /// public static bool IsFlagsEnum() { - if (!IsEnum()) + if (!IsEnumType()) throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name)); return IsFlagsEnum(typeof(T)); @@ -177,7 +188,7 @@ namespace ICD.Common.Utils if (type == null) throw new ArgumentNullException("type"); - if (!IsEnum(type)) + if (!IsEnumType(type)) throw new InvalidOperationException(string.Format("{0} is not an enum", type.Name)); return type @@ -195,9 +206,6 @@ namespace ICD.Common.Utils /// public static T GetFlagsIntersection(params T[] values) { - if (!IsEnum()) - throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name)); - if (values.Length == 0) return GetNoneValue(); @@ -216,8 +224,8 @@ namespace ICD.Common.Utils /// public static IEnumerable GetFlags(T value) { - if (!IsEnum()) - throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name)); + if (!IsEnum(value)) + throw new ArgumentException(string.Format("{0} is not an enum", value == null ? "NULL" : value.GetType().Name), "value"); return GetValues().Where(e => HasFlag(value, e)); } @@ -230,8 +238,8 @@ namespace ICD.Common.Utils /// public static IEnumerable GetFlagsExceptNone(T value) { - if (!IsEnum()) - throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name)); + if (!IsEnum(value)) + throw new ArgumentException(string.Format("{0} is not an enum", value == null ? "NULL" : value.GetType().Name), "value"); T none = GetNoneValue(); return GetFlags(value).Except(none); @@ -244,7 +252,7 @@ namespace ICD.Common.Utils /// public static T GetFlagsAllValue() { - if (!IsEnum()) + if (!IsEnumType()) throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name)); int output = GetValues().Aggregate(0, (current, value) => current | (int)(object)value); @@ -260,8 +268,11 @@ namespace ICD.Common.Utils /// public static bool HasFlag(T value, T flag) { - if (!IsEnum()) - throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name)); + if (!IsEnum(value)) + 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)); } @@ -275,8 +286,11 @@ namespace ICD.Common.Utils /// public static bool HasFlags(T value, T flags) { - if (!IsEnum()) - throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name)); + if (!IsEnum(value)) + 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)); } @@ -289,8 +303,8 @@ namespace ICD.Common.Utils /// public static bool HasSingleFlag(T value) { - if (!IsEnum()) - throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name)); + if (!IsEnum(value)) + throw new ArgumentException(string.Format("{0} is not an enum", value == null ? "NULL" : value.GetType().Name), "value"); return (int)(object)value != (int)(object)GetNoneValue() && !HasMultipleFlags(value); } @@ -302,8 +316,8 @@ namespace ICD.Common.Utils /// public static bool HasMultipleFlags(T value) { - if (!IsEnum()) - throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name)); + if (!IsEnum(value)) + throw new ArgumentException(string.Format("{0} is not an enum", value == null ? "NULL" : value.GetType().Name), "value"); return HasMultipleFlags((int)(object)value); } @@ -331,7 +345,7 @@ namespace ICD.Common.Utils /// public static T Parse(string data, bool ignoreCase) { - if (!IsEnum()) + if (!IsEnumType()) throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name)); try @@ -355,7 +369,7 @@ namespace ICD.Common.Utils /// public static bool TryParse(string data, bool ignoreCase, out T result) { - if (!IsEnum()) + if (!IsEnumType()) throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name)); result = default(T); @@ -379,8 +393,8 @@ namespace ICD.Common.Utils /// public static Enum ToEnum(T value) { - if (!IsEnum()) - throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name)); + if (!IsEnum(value)) + throw new ArgumentException(string.Format("{0} is not an enum", value == null ? "NULL" : value.GetType().Name), "value"); return ToEnum((object)value); } diff --git a/ICD.Common.Utils/Utils/Xml/XmlUtils.cs b/ICD.Common.Utils/Utils/Xml/XmlUtils.cs index 5413c94..5196045 100644 --- a/ICD.Common.Utils/Utils/Xml/XmlUtils.cs +++ b/ICD.Common.Utils/Utils/Xml/XmlUtils.cs @@ -435,7 +435,7 @@ namespace ICD.Common.Utils.Xml [PublicAPI] public static T ReadChildElementContentAsEnum(string xml, string childElement, bool ignoreCase) { - if (!EnumUtils.IsEnum()) + if (!EnumUtils.IsEnumType()) throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name)); string child = GetChildElementAsString(xml, childElement); @@ -595,7 +595,7 @@ namespace ICD.Common.Utils.Xml public static T? TryReadChildElementContentAsEnum(string xml, string childElement, bool ignoreCase) where T : struct { - if (!EnumUtils.IsEnum()) + if (!EnumUtils.IsEnumType()) throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name)); T output; @@ -614,7 +614,7 @@ namespace ICD.Common.Utils.Xml [PublicAPI] public static bool TryReadChildElementContentAsEnum(string xml, string childElement, bool ignoreCase, out T output) { - if (!EnumUtils.IsEnum()) + if (!EnumUtils.IsEnumType()) throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name)); output = default(T); @@ -709,7 +709,7 @@ namespace ICD.Common.Utils.Xml [PublicAPI] public static T ReadElementContentAsEnum(string xml, bool ignoreCase) { - if (!EnumUtils.IsEnum()) + if (!EnumUtils.IsEnumType()) throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name)); using (IcdXmlReader reader = new IcdXmlReader(xml))