自定义配置文件读取
来源:互联网 发布: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
- 自定义配置文件读取
- spring 读取自定义配置文件
- springboot读取自定义配置文件
- SpringBoot 读取自定义配置文件
- Spring读取自定义properties配置文件
- Spring自定义配置文件读取类
- 读取配置文件和自定义配置文件(python实现)
- Springboot 之 自定义配置文件及读取配置文件
- Springboot 之 自定义配置文件及读取配置文件
- Springboot 之 自定义配置文件及读取配置文件
- Springboot 之 自定义配置文件及读取配置文件
- Springboot 之 自定义配置文件及读取配置文件
- Springboot 之 自定义配置文件及读取配置文件
- linux读取自定义的配置文件方法
- 应用程序配置文件中读取自定义配置节
- Struts2读取自定义properties配置文件及国际化
- python读取和自定义配置文件的教程
- 读取 src 目录下 自定义配置文件
- 黑马程序员——面向对象
- 迁移OCR、表决磁盘和asm spfile到其他磁盘组中
- iOS9 之后的UIAlertView 的写法
- 周边公交站台接口 查询附近的公交站台
- C字符串转JNI字符串
- 自定义配置文件读取
- iOS开发系列--音频播放、录音、视频播放、拍照、视频录制
- 安卓设置布局边框
- 我的记事本项目之路(二)
- 文本属性Attributes
- 机器学习(5)——决策树(下)算法实现
- oracle数据库ORA-00600:internalerrorcode,arguments:[keltnfy-ldmInit],[46],[1],[],[],[],[],[])
- pl-------------linux执行.pl文件
- Java并发编程 之 线程池核心ThreadPoolExecutor