mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-01-11 19:44:55 +00:00
Validating enum types in xml utils
This commit is contained in:
@@ -21,7 +21,7 @@ namespace ICD.Common.Utils
|
||||
/// Returns true if the given type is an enum.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
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
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <returns></returns>
|
||||
private static bool IsEnum<T>()
|
||||
public static bool IsEnum<T>()
|
||||
{
|
||||
return IsEnum(typeof(T));
|
||||
}
|
||||
|
||||
@@ -435,6 +435,9 @@ namespace ICD.Common.Utils.Xml
|
||||
[PublicAPI]
|
||||
public static T ReadChildElementContentAsEnum<T>(string xml, string childElement, bool ignoreCase)
|
||||
{
|
||||
if (!EnumUtils.IsEnum<T>())
|
||||
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<T>(string xml, string childElement, bool ignoreCase)
|
||||
where T : struct
|
||||
{
|
||||
if (!EnumUtils.IsEnum<T>())
|
||||
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<T>(string xml, string childElement, bool ignoreCase, out T output)
|
||||
{
|
||||
if (!EnumUtils.IsEnum<T>())
|
||||
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<T>(string xml, bool ignoreCase)
|
||||
{
|
||||
if (!EnumUtils.IsEnum<T>())
|
||||
throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name));
|
||||
|
||||
using (IcdXmlReader reader = new IcdXmlReader(xml))
|
||||
{
|
||||
reader.Read();
|
||||
|
||||
Reference in New Issue
Block a user