Custom config items

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="accounts" type="MyApp.Config.AccountsSection, MyApp" />
    </configSections>
    <appSettings></appSettings>
    <accounts host="myhost">
        <add user="abc" password="def" />
        <add user="123" password="456" />
    </accounts>
</configuration>
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
 
namespace MyApp.Config {
    public class AccountsSection : ConfigurationSection {
        [ConfigurationProperty("", IsRequired = true, IsDefaultCollection = true)]
        public AccountCollection Instances {
            get { return (AccountCollection)this[""]; }
            set { this[""] = value; }
        }
        [ConfigurationProperty("host", IsRequired = true)]
        public string Host {
            get { return (string)this["host"]; }
            set { this["host"] = value; }
        }
    }
    public class AccountCollection : ConfigurationElementCollection {
        protected override ConfigurationElement CreateNewElement() {
            return new Account();
        }
        protected override object GetElementKey(ConfigurationElement element) {
            return ((Account)element).User;
        }
    }
    public class Account : ConfigurationElement {
        [ConfigurationProperty("user", IsKey = true, IsRequired = true)]
        public string User {
            get { return (string)base["user"]; }
            set { base["user"] = value; }
        }
        [ConfigurationProperty("password", IsRequired = true)]
        public string Password {
            get { return (string)base["password"]; }
            set { base["password"] = value; }
        }
        public override string ToString() {
            return string.Format("{0} {1}", User, Password);
        }
    }
    public static class AccountConfig {
        private static Dictionary<string, Account> _instances;
        static AccountConfig() {
            _instances = new Dictionary<string, Account>();
            foreach (Account account in ((AccountsSection)ConfigurationManager.GetSection("accounts")).Instances) {
                _instances.Add(account.User, account);
            }
        }
        public static AccountsSection GetSection() {
            return (AccountsSection)ConfigurationManager.GetSection("accounts");
        }
        public static Account GetAccountByUser(string user) {
            return _instances[user];
        }
        public static Account[] GetAll() {
            return _instances.Values.ToArray();
        }
    }
}
using System;
namespace MyApp {
    class Program {
        static void Main(string[] args) {
            var items = Config.AccountConfig.GetAll();
            foreach (var item in items) {
                Console.WriteLine(item);
            }
            var section = Config.AccountConfig.GetSection();
            Console.WriteLine("host={0}", section.Host);
        }
    }
}

Comments

Popular posts from this blog

C# Record Serialization

Add timestamp to photo using ImageMagick

Read/write large blob to SQL Server from C#