One of the ConfigurationProperty I have in my ConfigurationSection is an ENUM. When .net parses this enum string value from the config file, an exception will be thrown if the case does not match exactly.
Is there away to ignore case when parsing this value?
If value is the string representation of the name of an enumeration value, the comparison of value with enumeration names is case-sensitive.
Use the Enum. IsDefined() method to check if a given string name or integer value is defined in a specified enumeration. Thus, the conversion of String to Enum can be implemented using the Enum. Parse ( ) and Enum.
The Parse method in Enum converts the string representation of the name or numeric value of enum constants to an equivalent enumerated object.
The enum can be of any numeric data type such as byte, sbyte, short, ushort, int, uint, long, or ulong. However, an enum cannot be a string type. Specify the type after enum name as : type .
Try using this:
Enum.Parse(enum_type, string_value, true);
Last param set to true tells to ignore string casing when parsing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With