Pages

Custom Application Configuration

Custom Configuration
Create custom settings in the application settings as below;
    <configSections>
      <!--your other sectiongroups & sections -->
      <section name="viewTypeSettings" type="ConfigTest.ViewTypeSettings,ConfigTest" />        
    </configSections>
 

In order create custom settings in your application config file; create a class which derived from ConfigurationSection class as below


    public class ViewTypeSettings : ConfigurationSection
    {
        static ViewTypeSettings()
        {
            Settings = ConfigurationManager.GetSection("viewTypeSettings"as ViewTypeSettings;
        }
        public static ViewTypeSettings Settings { getprivate set; }
        [ConfigurationProperty("viewTypes")]
        public ViewTypeCollection ViewTypes { get { return this["viewTypes"as ViewTypeCollection; } }
    }
 
Now you can create custom elements as below
 <viewTypeSettings >
    <viewTypes>
      <viewType name="Phone" deviceIdentifiers="iPhone,iPod,Droid,Blackberry"></viewType>
      <viewType name="Tablet" deviceIdentifiers="iPad,Playbook,Transformer,Xoom"></viewType>
    </viewTypes>
  </viewTypeSettings>
 

In order to create custom elements, create a class which derived from ConfigurationElement Class   as below

 
    public class ViewType : ConfigurationElement
    {
        [ConfigurationProperty("name", IsRequired = true)]
        //[StringValidator(InvalidCharacters = "  ~!@#$%^&*()[]{}/;’\"|\\", MinLength = 1, MaxLength = 256)]
        [StringValidator(InvalidCharacters = " ~!@#$%^&*()[]{}/;’\"|\\", MinLength = 0, MaxLength = 256)]
        public string Name
        {
            get { return this["name"as string; }
            set { this["name"] = value; }
        }
 
        [ConfigurationProperty("deviceIdentifiers", IsRequired = true)]
        [StringValidator(InvalidCharacters = " ~!@#$%^&*()[]{}/;’\"|\\", MinLength = 0, MaxLength = 256)]
        public string DeviceIdentifiers
        {
            get { return this["deviceIdentifiers"as string; }
            set { this["deviceIdentifiers"] = value; }
        }
 
    }
 

 In order to create custom element array , create a class which derived from ConfigurationElementCollection   as below

 
    [ConfigurationCollection(typeof (ViewType), AddItemName = "viewType",
        CollectionType = ConfigurationElementCollectionType.BasicMap)]
    public class ViewTypeCollection : ConfigurationElementCollectionIEnumerable<ViewType
    {
        #region Constructors
 
        static ViewTypeCollection()
        {
            _properties = new ConfigurationPropertyCollection();
        }
 
        #endregion
 
        #region Fields
 
        private static readonly ConfigurationPropertyCollection _properties;
 
        #endregion
 
        #region Properties
 
        protected override ConfigurationPropertyCollection Properties
        {
            get { return _properties; }
        }
 
        public override ConfigurationElementCollectionType CollectionType
        {
            get { return ConfigurationElementCollectionType.AddRemoveClearMap; }
        }
 
        #endregion
 
        #region indexer
 
        public ViewType this[int index]
        {
            get { return (ViewType) BaseGet(index); }
            set
            {
                if (BaseGet(index) != null)
                {
                    BaseRemoveAt(index);
                }
                base.BaseAdd(index, value);
            }
        }
 
        public new ViewType this[string name]
        {
            get { return (ViewType) BaseGet(name); }
        }
        
        #endregion
 
        #region overriden methods
 
        protected override ConfigurationElement CreateNewElement()
        {
            return new ViewType();
        }
 
        protected override object GetElementKey(ConfigurationElement element)
        {
            return (element as ViewType).Name;
        }
 
        public new IEnumerator<ViewType> GetEnumerator()
        {
            var baselsit = base.GetEnumerator();
 
            while (baselsit.MoveNext())
            {
                yield return baselsit.Current as ViewType;
            }
        }
 
        #endregion
    }