comparison EnumTools.cs @ 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
children
comparison
equal deleted inserted replaced
62:6e46a62ad9b8 63:8fe11cd7d3bf
1 // This file (XmlTools.cs) is a part of the IBBoard library and is copyright 2009 IBBoard
2 //
3 // 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.
4 //
5
6 using System;
7
8 namespace IBBoard
9 {
10 public class EnumTools
11 {
12 /// <summary>
13 /// 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.
14 /// </summary>
15 /// <param name="enumString">
16 /// The enum text string to parse
17 /// </param>
18 /// <returns>
19 /// A typed enum value parsed from the text string
20 /// </returns>
21 public static T ParseEnum<T>(string enumString)
22 {
23 return ParseEnum<T>(enumString, true);
24 }
25
26 /// <summary>
27 /// 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.
28 /// </summary>
29 /// <param name="enumString">
30 /// The enum text string to parse
31 /// </param>
32 /// <param name="ignoreCase">
33 /// <code>True</code> if parsing should be case-insensitive, else <code>false</code>
34 /// </param>
35 /// <returns>
36 /// A typed enum value parsed from the text string
37 /// </returns>
38 public static T ParseEnum<T>(string enumString, bool ignoreCase)
39 {
40 return (T) Enum.Parse(typeof (T), enumString, ignoreCase);
41 }
42 }
43 }