From 03cb49ba943c06a7f846b92f994b70f8bf5f9196 Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Sun, 2 Jul 2017 17:47:24 -0400 Subject: [PATCH] Validating enum types in xml utils --- ICD.Common.Utils/Utils/EnumUtils.cs | 4 ++-- ICD.Common.Utils/Utils/Xml/XmlUtils.cs | 12 ++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/ICD.Common.Utils/Utils/EnumUtils.cs b/ICD.Common.Utils/Utils/EnumUtils.cs index ef7ec7f..4d540ee 100644 --- a/ICD.Common.Utils/Utils/EnumUtils.cs +++ b/ICD.Common.Utils/Utils/EnumUtils.cs @@ -21,7 +21,7 @@ namespace ICD.Common.Utils /// Returns true if the given type is an enum. /// /// - private static bool IsEnum(Type type) + public static bool IsEnum(Type type) { return type == typeof(Enum) || type #if !SIMPLSHARP @@ -35,7 +35,7 @@ namespace ICD.Common.Utils /// /// /// - private static bool IsEnum() + public static bool IsEnum() { return IsEnum(typeof(T)); } diff --git a/ICD.Common.Utils/Utils/Xml/XmlUtils.cs b/ICD.Common.Utils/Utils/Xml/XmlUtils.cs index 3f30879..5413c94 100644 --- a/ICD.Common.Utils/Utils/Xml/XmlUtils.cs +++ b/ICD.Common.Utils/Utils/Xml/XmlUtils.cs @@ -435,6 +435,9 @@ namespace ICD.Common.Utils.Xml [PublicAPI] public static T ReadChildElementContentAsEnum(string xml, string childElement, bool ignoreCase) { + if (!EnumUtils.IsEnum()) + throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name)); + string child = GetChildElementAsString(xml, childElement); using (IcdXmlReader reader = new IcdXmlReader(child)) { @@ -592,6 +595,9 @@ namespace ICD.Common.Utils.Xml public static T? TryReadChildElementContentAsEnum(string xml, string childElement, bool ignoreCase) where T : struct { + if (!EnumUtils.IsEnum()) + throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name)); + T output; return TryReadChildElementContentAsEnum(xml, childElement, ignoreCase, out output) ? output : (T?)null; } @@ -608,6 +614,9 @@ namespace ICD.Common.Utils.Xml [PublicAPI] public static bool TryReadChildElementContentAsEnum(string xml, string childElement, bool ignoreCase, out T output) { + if (!EnumUtils.IsEnum()) + throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name)); + output = default(T); try @@ -700,6 +709,9 @@ namespace ICD.Common.Utils.Xml [PublicAPI] public static T ReadElementContentAsEnum(string xml, bool ignoreCase) { + if (!EnumUtils.IsEnum()) + throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name)); + using (IcdXmlReader reader = new IcdXmlReader(xml)) { reader.Read();