ConfigurationSection multiple enum values

mimo picture mimo · Nov 1, 2012 · Viewed 7.7k times · Source

Is there a way to set multiple enum values in a configuration section?

Like you do in .net object.Filter = Filter.Update | Filter.Create;

<wacther filter="update, created"/>

Is something like that supported?

Answer

fsimonazzi picture fsimonazzi · Nov 1, 2012

It just works out of the box:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var section = (MySection)ConfigurationManager.GetSection("mySection");
            Console.WriteLine(section.Enum);
        }
    }

    public class MySection : ConfigurationSection
    {
        [ConfigurationProperty("enum")]
        public MyEnum Enum
        {
            get { return (MyEnum)this["enum"]; }
            set { this["enum"] = value; }
        }
    }

    [Flags]
    public enum MyEnum
    {
        None = 0,
        Foo = 1,
        Bar = 2,
        Baz = 4
    }
}


<configSections>
  <section name="mySection" type="ConsoleApplication1.MySection, ConsoleApplication1"/>
</configSections>

<mySection enum="Foo, Bar"/>

Prints: Foo, Bar