Thursday, March 21, 2013

[C#] How to read custom config Section in C#

step 1. Create a class to handle custom configuration elements. in this sample called "MyAttribute"

and this class will be responsible for reading following element from config file



<add myAttributeId="6" myAttributes="1" />



using System.Configuration;

namespace MyApp.Configurations
{
    public class MyAttributeConfigurationElement : ConfigurationElement
    {
        [ConfigurationProperty("myAttributeId", IsRequired = true)]
        public int MyAttributeId
        {
            get
            {
                return (int)this["myAttributeId"];
            }
            set
            {
                this["myAttributeId"] = value;
            }
        }


        [ConfigurationProperty("myAttributes", IsRequired = true)]
        public string MyAttributes
        {
            get
            {
                return (string)this["myAttributes"];
            }
            set
            {
                this["myAttributes"] = value;
            }
        }
    }
}

step 2 create class to handle config element collection. as follows


using System.Collections;
using System.Collections.Generic;
using System.Configuration;

namespace MyApp.Configurations
{
    public class MyAttributeConfigurationCollection : ConfigurationElementCollection, IEnumerable
    {
        ///





        ///
        ///
        ///
        ///
        public MyAttributeConfigurationElement this[int index]
        {
            get
            {
                return BaseGet(index) as MyAttributeConfigurationElement;
            }
            set
            {
                if (BaseGet(index) != null)
                {
                    BaseRemoveAt(index);
                }
                BaseAdd(index, value);
            }
        }

        ///





        ///
        ///
        public IEnumerable Elements
        {
            get
            {
                for (var i = 0; i < Count; ++i)
                {
                    yield return this[i];
                }
            }
        }

        ///





        ///
        ///
        ///
        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        ///





        ///
        ///
        ///
        public new IEnumerator GetEnumerator()
        {
            for (var i = 0; i < Count; ++i)
            {
                yield return this[i];
            }
        }

        ///





        ///
        ///
        ///
        protected override ConfigurationElement CreateNewElement()
        {
            return new MyAttributeConfigurationElement();
        }

        ///





        ///
        ///
        ///
        ///
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((MyAttributeConfigurationElement)element).MyAttributeId;
        }
    }
}




step 3. Create Class to handle custom configuration section. Here called "MyAttributeSection"


using System.Configuration;

namespace MyApp.Configurations
{
    public class MyAttributeConfiguration : ConfigurationSection
    {
        private static readonly MyAttributeConfiguration MyAttributeConfigSection
            = ConfigurationManager.GetSection("myAttributeConfigSection") as MyAttributeConfiguration;

        public static MyAttributeConfiguration Section
        {
            get
            {
                return MyAttributeConfigSection;
            }
        }


        [ConfigurationProperty("myAttributeConfig")]
        public MyAttributeConfigurationCollection MyAttributeConfig
        {
            get
            {
                return this["myAttributeConfig"] as MyAttributeConfigurationCollection;
            }
        }
    }
}

step 4 create a config file entry to be read by the classes we have created so far



<configSections>
    <section name="myAttributeConfigSection" type="MyApp.Configurations.MyAttributeConfiguration,MyApp" />
</configSections>
<myAttributeConfigSection>
    <myAttributeConfig>
      <add myAttributeId="6" myAttributes="1" />
    </myAttributeConfig>
</myAttributeConfigSection>





No comments:

Post a Comment