Mercurial > repos > IBBoard
changeset 63:8fe11cd7d3bf
Re #25: Enum parsing
* Extract enum parsing to separate class
* Add more info to XmlTools exceptions and use new class
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Mon, 30 Nov 2009 21:00:05 +0000 |
parents | 6e46a62ad9b8 |
children | 70d6c2a5d99e |
files | EnumTools.cs IBBoard.csproj Xml/XmlTools.cs |
diffstat | 3 files changed, 48 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EnumTools.cs Mon Nov 30 21:00:05 2009 +0000 @@ -0,0 +1,43 @@ +// This file (XmlTools.cs) is a part of the IBBoard library and is copyright 2009 IBBoard +// +// The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license. +// + +using System; + +namespace IBBoard +{ + public class EnumTools + { + /// <summary> + /// Takes a string and parses it as the required enum, returning the typed enum object. Parsing is case-insensitive. Throws a <code>ArgumentException</code> if the argument is not a valid enum value. + /// </summary> + /// <param name="enumString"> + /// The enum text string to parse + /// </param> + /// <returns> + /// A typed enum value parsed from the text string + /// </returns> + public static T ParseEnum<T>(string enumString) + { + return ParseEnum<T>(enumString, true); + } + + /// <summary> + /// Takes a string and parses it as the required enum, returning the typed enum object. Parsing can be case-sensitive or case-insensitive. Throws a <code>ArgumentException</code> if the argument is not a valid enum value. + /// </summary> + /// <param name="enumString"> + /// The enum text string to parse + /// </param> + /// <param name="ignoreCase"> + /// <code>True</code> if parsing should be case-insensitive, else <code>false</code> + /// </param> + /// <returns> + /// A typed enum value parsed from the text string + /// </returns> + public static T ParseEnum<T>(string enumString, bool ignoreCase) + { + return (T) Enum.Parse(typeof (T), enumString, ignoreCase); + } + } +}
--- a/IBBoard.csproj Mon Nov 30 20:46:54 2009 +0000 +++ b/IBBoard.csproj Mon Nov 30 21:00:05 2009 +0000 @@ -134,6 +134,7 @@ <Compile Include="Limits\ILimit.cs" /> <Compile Include="Limits\IPercentageLimit.cs" /> <Compile Include="Limits\INumericLimit.cs" /> + <Compile Include="EnumTools.cs" /> </ItemGroup> <ItemGroup> <Content Include="libs\log4net.dll" />
--- a/Xml/XmlTools.cs Mon Nov 30 20:46:54 2009 +0000 +++ b/Xml/XmlTools.cs Mon Nov 30 21:00:05 2009 +0000 @@ -110,13 +110,15 @@ public static T GetEnumValueFromAttribute<T>(XmlElement elem, string attributeName, bool ignoreCase) { + string attribValue = elem.GetAttribute (attributeName); + try { - return (T) Enum.Parse(typeof (T), elem.GetAttribute(attributeName), ignoreCase); + return EnumTools.ParseEnum<T>(attribValue, ignoreCase); } catch(ArgumentException) { - throw new FormatException(String.Format("Attribute '{0}' of {1} with ID {2} was not a valid {3} enum", attributeName, elem.Name, elem.GetAttribute("id"), typeof(T).Name)); + throw new FormatException(String.Format("Attribute '{0}' with value {1} for {2} with ID '{3}' was not a valid {4} enum", attributeName, attribValue, elem.Name, elem.GetAttribute("id"), typeof(T).Name)); } }