Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.net Custom Configuration How to case insensitive parse an enum ConfigurationProperty

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?

like image 347
Koda Avatar asked Jan 19 '12 08:01

Koda


People also ask

Is enum parse case sensitive C#?

If value is the string representation of the name of an enumeration value, the comparison of value with enumeration names is case-sensitive.

How to Parse string to enum c#?

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.

What is enum parse in C#?

The Parse method in Enum converts the string representation of the name or numeric value of enum constants to an equivalent enumerated object.

Can C# enums be strings?

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 .


1 Answers

Try using this:

Enum.Parse(enum_type, string_value, true); 

Last param set to true tells to ignore string casing when parsing.

like image 170
Marco Avatar answered Sep 23 '22 00:09

Marco