自定义配置文件读取

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值