mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-16 13:15:07 +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.
|
/// Returns true if the given type is an enum.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
private static bool IsEnum(Type type)
|
public static bool IsEnum(Type type)
|
||||||
{
|
{
|
||||||
return type == typeof(Enum) || type
|
return type == typeof(Enum) || type
|
||||||
#if !SIMPLSHARP
|
#if !SIMPLSHARP
|
||||||
@@ -35,7 +35,7 @@ namespace ICD.Common.Utils
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <typeparam name="T"></typeparam>
|
/// <typeparam name="T"></typeparam>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
private static bool IsEnum<T>()
|
public static bool IsEnum<T>()
|
||||||
{
|
{
|
||||||
return IsEnum(typeof(T));
|
return IsEnum(typeof(T));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -435,6 +435,9 @@ 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>())
|
||||||
|
throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name));
|
||||||
|
|
||||||
string child = GetChildElementAsString(xml, childElement);
|
string child = GetChildElementAsString(xml, childElement);
|
||||||
using (IcdXmlReader reader = new IcdXmlReader(child))
|
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)
|
public static T? TryReadChildElementContentAsEnum<T>(string xml, string childElement, bool ignoreCase)
|
||||||
where T : struct
|
where T : struct
|
||||||
{
|
{
|
||||||
|
if (!EnumUtils.IsEnum<T>())
|
||||||
|
throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name));
|
||||||
|
|
||||||
T output;
|
T output;
|
||||||
return TryReadChildElementContentAsEnum(xml, childElement, ignoreCase, out output) ? output : (T?)null;
|
return TryReadChildElementContentAsEnum(xml, childElement, ignoreCase, out output) ? output : (T?)null;
|
||||||
}
|
}
|
||||||
@@ -608,6 +614,9 @@ 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>())
|
||||||
|
throw new ArgumentException(string.Format("{0} is not an enum", typeof(T).Name));
|
||||||
|
|
||||||
output = default(T);
|
output = default(T);
|
||||||
|
|
||||||
try
|
try
|
||||||
@@ -700,6 +709,9 @@ 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>())
|
||||||
|
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))
|
||||||
{
|
{
|
||||||
reader.Read();
|
reader.Read();
|
||||||
|
|||||||
Reference in New Issue
Block a user