自定义配置文件读取

来源:互联网 发布:ug三维制图软件 编辑:程序博客网 时间:2024/05/16 18:10

1.自定义配置文件

<CommonPlatformConfig>  <default factory="sql"></default>  <factorys>       <add name="mysql" assembly="myhello.data" classname="MySqlDbFactory"/>       <add name="oracle" assembly="oraclehello.data" classname="OracleDbFactory"/>  </factorys></CommonPlatformConfig>

2.自定义配置类DbFactorySection

    //<dbFactory> 继承于ConfigurationSection    public class DbFactorySection : ConfigurationSection    {        [ConfigurationProperty("default")]        public DefaultElement DefaultElement        {            get            {                return this["default"] as DefaultElement;            }            set            {                this["default"] = value;            }        }        [ConfigurationProperty("factorys")]        public FactoryListElements FactoryList        {            get            {                return this["factorys"] as FactoryListElements;            }            set            {                this["factorys"] = value;            }        }    }

3.对应配置属性

   public class DefaultElement : ConfigurationElement    {        [ConfigurationProperty("factory")]        public string Factory        {            get            {                return this["factory"] as string;            }            set            {                this["factory"] = value;            }        }    }    //<factorys> 子元素    public class FactoryElement : ConfigurationElement    {        [ConfigurationProperty("name")]        public string Name        {            get            {                return this["name"] as string;            }            set            {                this["name"] = value;            }        }        [ConfigurationProperty("assembly")]        public string Assembly        {            get            {                return this["assembly"] as string;            }            set            {                this["assembly"] = value;            }        }        [ConfigurationProperty("classname")]        public string ClassName        {            get            {                return this["classname"] as string;            }            set            {                this["classname"] = value;            }        }    }    public class FactoryListElements : ConfigurationElementCollection    {        protected override ConfigurationElement CreateNewElement()        {            return new FactoryElement();        }        protected override object GetElementKey(ConfigurationElement element)        {            return ((FactoryElement) element).Name;        }        public FactoryElement this[string name]        {            get            {                return BaseGet(name) as FactoryElement;            }        }    }

4.自定义配置实例

        /// <summary>        /// 公用平台配置实例        /// </summary>        private static DbFactorySection instance;        /// <summary>        /// 配置属性        /// </summary>        public static DbFactorySection Instance        {            get            {                // Uses "Lazy initialization"                if (instance == null)                {                    instance = (DbFactorySection)ConfigurationManager.GetSection("CommonPlatformConfig");                }                return instance;            }        }        public static DefaultElement GetDefaultElement()        {            DbFactorySection db = (DbFactorySection) ConfigurationManager.GetSection("CommonPlatformConfig");            return db.DefaultElement;//Instance.DefaultElement;        }        public static FactoryElement GetFactoryElement(string name)        {            return Instance.FactoryList[name];        }

5.APP.CONFIG配置

<?xml version="1.0" encoding="utf-8" ?><configuration>  <configSections>    <!--公用平台的配置Section-->    <section name="CommonPlatformConfig" type="易车配置文件读取.DbFactorySection, 易车配置文件读取" allowDefinition="MachineToApplication" restartOnExternalChanges="true"/>  </configSections>  <CommonPlatformConfig configSource="CommonPlatformConfig.config"/></configuration>

6.调用

 DefaultElement defaultElement=CommonPlatformConfiguration.GetDefaultElement(); var d=CommonPlatformConfiguration.GetFactoryElement("mysql");
0 0
原创粉丝点击