92

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?

ahsteele
  • 26,243
  • 28
  • 134
  • 248
Koda
  • 1,709
  • 3
  • 14
  • 15

3 Answers3

287

Try using this:

Enum.Parse(enum_type, string_value, true);

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

Marco
  • 56,740
  • 14
  • 129
  • 152
  • 6
    Read Koda's comment to the original question. It's using ConfigurationPropertyAttribute, which parses automatically in case-sensitive mode. Enum.Parse is not used directly. The accepted answer (inheriting from ConfigurationConvertorBase) is the correct answer. – Matt Sach Jan 05 '16 at 12:04
  • 13
    @MattSach Right answer to the wrong question, I suppose. This is one of the first couple results for ".net case insensitive enum parse" on Google, sadly. – jpmc26 Jan 29 '16 at 22:18
  • 2
    Notice: Enum.TryParse has a signature with the same parameter, working as well – ukod Nov 13 '20 at 06:12
  • Pass a number and you'll see it end up working different (As designed, but not all need it) – Deepak Feb 08 '23 at 05:36
48

You can use ConfigurationConverterBase to make a custom configuration converter, see http://msdn.microsoft.com/en-us/library/system.configuration.configurationconverterbase.aspx

this will do the job:

 public class CaseInsensitiveEnumConfigConverter<T> : ConfigurationConverterBase
    {
        public override object ConvertFrom(
        ITypeDescriptorContext ctx, CultureInfo ci, object data)
        {
            return Enum.Parse(typeof(T), (string)data, true);
        }
    }

and then on your property:

[ConfigurationProperty("unit", DefaultValue = MeasurementUnits.Pixel)]
[TypeConverter(typeof(CaseInsensitiveEnumConfigConverter<MeasurementUnits>))]
public MeasurementUnits Unit { get { return (MeasurementUnits)this["unit"]; } }

public enum MeasurementUnits
{
        Pixel,
        Inches,
        Points,
        MM,
}
ziv
  • 3,641
  • 3
  • 21
  • 26
25

MyEnum.TryParse() has an IgnoreCase parameter, set it true.

http://msdn.microsoft.com/en-us/library/dd991317.aspx

UPDATE: Defining the configuration section like this should work

public class CustomConfigurationSection : ConfigurationSection
    {
      [ConfigurationProperty("myEnumProperty", DefaultValue = MyEnum.Item1, IsRequired = true)]
      public MyEnum SomeProperty
      {
        get
        {
          MyEnum tmp;
          return Enum.TryParse((string)this["myEnumProperty"],true,out tmp)?tmp:MyEnum.Item1;
        }
        set
        { this["myEnumProperty"] = value; }
      }
    }
tbt
  • 718
  • 5
  • 7
  • 1
    Yes I am aware that Enum.Parse has an ignorecase flag. But .net parses this ConfigurationProperty automatically when I use the ConfigurationPropertyAttribute. – Koda Jan 19 '12 at 08:31
  • Thank you.This solved my problem without having to write additional code. – nikhil Aug 17 '18 at 01:13