diff --git a/ICD.Common.Utils.Tests/EnumUtilsTest.cs b/ICD.Common.Utils.Tests/EnumUtilsTest.cs
index c36a4db..48820b4 100644
--- a/ICD.Common.Utils.Tests/EnumUtilsTest.cs
+++ b/ICD.Common.Utils.Tests/EnumUtilsTest.cs
@@ -273,20 +273,6 @@ namespace ICD.Common.Utils.Tests
Assert.AreEqual(eTestFlagsEnum.None, outputB);
}
- [Test]
- public void ToEnumGenericTest()
- {
- Assert.AreEqual(eTestEnum.A, EnumUtils.ToEnum(eTestEnum.A));
- Assert.AreNotEqual(eTestEnum.B, EnumUtils.ToEnum(eTestEnum.A));
- }
-
- [Test]
- public void ToEnumTest()
- {
- Assert.AreEqual(eTestEnum.A, EnumUtils.ToEnum((object)eTestEnum.A));
- Assert.AreNotEqual(eTestEnum.B, EnumUtils.ToEnum((object)eTestEnum.A));
- }
-
#endregion
}
}
diff --git a/ICD.Common.Utils.Tests/Xml/XmlUtilsTest.cs b/ICD.Common.Utils.Tests/Xml/XmlUtilsTest.cs
index 6d3025c..027455d 100644
--- a/ICD.Common.Utils.Tests/Xml/XmlUtilsTest.cs
+++ b/ICD.Common.Utils.Tests/Xml/XmlUtilsTest.cs
@@ -253,6 +253,7 @@ namespace ICD.Common.Utils.Tests.Xml
[TestCase("A", "Child", true, eTestEnum.A)]
[TestCase("A, B", "Child", true, eTestEnum.A | eTestEnum.B)]
public void ReadChildElementContentAsEnumTest(string xml, string childElement, bool ignoreCase, T expected)
+ where T : struct, IConvertible
{
Assert.AreEqual(expected, XmlUtils.ReadChildElementContentAsEnum(xml, childElement, ignoreCase));
}
diff --git a/ICD.Common.Utils/EnumUtils.cs b/ICD.Common.Utils/EnumUtils.cs
index 729f5d6..781be84 100644
--- a/ICD.Common.Utils/EnumUtils.cs
+++ b/ICD.Common.Utils/EnumUtils.cs
@@ -25,12 +25,13 @@ namespace ICD.Common.Utils
s_EnumFlagsCache = new Dictionary>();
}
+ #region Validation
+
///
/// Returns true if the given type is an enum.
///
///
public static bool IsEnumType()
- where T : struct, IConvertible
{
return IsEnumType(typeof(T));
}
@@ -56,9 +57,34 @@ namespace ICD.Common.Utils
///
///
public static bool IsEnum(T value)
- where T : struct, IConvertible
{
- return IsEnumType(value.GetType());
+ return value != null && IsEnumType(value.GetType());
+ }
+
+ ///
+ /// Returns true if the given enum type has the Flags attribute set.
+ ///
+ ///
+ ///
+ public static bool IsFlagsEnum()
+ {
+ return IsFlagsEnum(typeof(T));
+ }
+
+ ///
+ /// Returns true if the given enum type has the Flags attribute set.
+ ///
+ ///
+ public static bool IsFlagsEnum(Type type)
+ {
+ if (type == null)
+ throw new ArgumentNullException("type");
+
+ return type
+#if !SIMPLSHARP
+ .GetTypeInfo()
+#endif
+ .IsDefined(typeof(FlagsAttribute), false);
}
///
@@ -73,21 +99,30 @@ namespace ICD.Common.Utils
if (!IsEnumType())
throw new InvalidOperationException(string.Format("{0} is not an enum", typeof(T).Name));
- if (!IsFlagsEnum())
- return GetValues().Any(v => v.Equals(value));
-
- int valueInt = (int)(object)value;
-
- // Check if all of the flag values are defined
- foreach (T flag in GetFlags(value))
- {
- int flagInt = (int)(object)flag;
- valueInt = valueInt - flagInt;
- }
-
- return valueInt == 0;
+ T all = GetFlagsAllValue();
+ return HasFlags(all, value);
}
+ ///
+ /// Returns true if the given value is defined as part of the given enum type.
+ ///
+ ///
+ ///
+ ///
+ public static bool IsDefined(Type type, int value)
+ {
+ if (type == null)
+ throw new ArgumentNullException("type");
+
+ if (!IsEnumType(type))
+ throw new InvalidOperationException(string.Format("{0} is not an enum", type.Name));
+
+ int all = GetFlagsAllValue(type);
+ return HasFlags(all, value);
+ }
+
+ #endregion
+
#region Values
///
@@ -117,7 +152,10 @@ namespace ICD.Common.Utils
///
public static IEnumerable GetValues(Type type)
{
- return GetValuesUncached(type).Cast();
+ if (type == null)
+ throw new ArgumentNullException("type");
+
+ return GetValuesUncached(type);
}
///
@@ -134,8 +172,11 @@ namespace ICD.Common.Utils
/// Gets the values from an enumeration without performing any caching. This is slow because of reflection.
///
///
- private static IEnumerable