随着项目的扩展,单独的key,value配置文件已经不能满足需求了

这里需要自定义配置节点,例如

1   <!--自定义  具体实体类配置问节点信息-->
2   <School Name="红旗小学" Number="1008" Address="北京市,西城区……"></School>

当然,这样的节点可以有多重获取方式,讲自己认为比较好的方式总结一下,仅供参考

可以将当前节点当做试题Model来配置,这样方便管理和操作,下面说明下如何进行转换

1、首先定义实体,因为需要转换,这里用到了configSections的section 节点,配置实体的命名空间

然后试题还需要继承ConfigurationSection类

 1 namespace Model
 2 {
 3     /// <summary>
 4     /// 学校实体
 5     /// </summary>
 6     public class School : ConfigurationSection
 7     {
 8         //获取属性名称
 9         [ConfigurationProperty("Name", IsRequired = false)]
10         public string Name
11         {
12             get
13             {
14                 //返回配置文件属性值
15                 return this["Name"].ToString();
16             }
17         }
18         [ConfigurationProperty("Number", IsRequired = false)]
19         public int Number
20         {
21             get
22             {
23                 int i = 0;
24                 int.TryParse(this["Number"].ToString(), out i);
25                 return i;
26             }
27         }
28         [ConfigurationProperty("Address", IsRequired = false)]
29         public string Address
30         {
31             get
32             {
33                 return this["Address"].ToString();
34             }
35         }
36     }
37 }

2、理由ExeConfigurationFileMap类将自定义配置文件转换为Configuration类

然后从Configuration类中获取section节点属性,转换为Model实体进行返回

 1  #region 初始化配置
 2 
 3         /// <summary>
 4         /// 初始化配置文件
 5         /// </summary>
 6         private static void ConfigDataLoad()
 7         {
 8             //获取文件路径
 9             string fileName = AppDomain.CurrentDomain.BaseDirectory + @"Configuration\TestModel.config";
10             if (File.Exists(fileName))
11             {
12                 ExeConfigurationFileMap file = new ExeConfigurationFileMap
13                 {
14                     ExeConfigFilename = fileName
15                 };
16                 //将文件转换为Configuration
17                 Configuration config = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None);
18 
19                 //初始化对象
20                 SetCustomModel(config);
21             }
22             else
23             {
24                 throw new Exception("配置文件不存在");
25             }
26         }
27         /// <summary>
28         /// 初始化值配置文件为实体Model
29         /// </summary>
30         private static void SetCustomModel(Configuration config)
31         {
32             _school = ((School)config.GetSection("School"));
33         }
34         #endregion

完整帮助类代码如下

 1     /// <summary>
 2     /// 自定义配置文件 实体转换
 3     /// </summary>
 4     public class ConfigToModelHelper
 5     {
 6         #region  初始化自定义节点为Model
 7         private static School _school;
 8         public static School School
 9         {
10             get
11             {
12                 if (_school == null)
13                 {
14                     ConfigDataLoad();
15                 }
16                 return _school;
17             }
18         }
19         #endregion
20 
21 
22         #region 初始化配置
23 
24         /// <summary>
25         /// 初始化配置文件
26         /// </summary>
27         private static void ConfigDataLoad()
28         {
29             //获取文件路径
30             string fileName = AppDomain.CurrentDomain.BaseDirectory + @"Configuration\TestModel.config";
31             if (File.Exists(fileName))
32             {
33                 ExeConfigurationFileMap file = new ExeConfigurationFileMap
34                 {
35                     ExeConfigFilename = fileName
36                 };
37                 //将文件转换为Configuration
38                 Configuration config = ConfigurationManager.OpenMappedExeConfiguration(file, ConfigurationUserLevel.None);
39 
40                 //初始化对象
41                 SetCustomModel(config);
42             }
43             else
44             {
45                 throw new Exception("配置文件不存在");
46             }
47         }
48         /// <summary>
49         /// 初始化值配置文件为实体Model
50         /// </summary>
51         private static void SetCustomModel(Configuration config)
52         {
53             _school = ((School)config.GetSection("School"));
54         }
55         #endregion
56 
57     }

调用:

1  School school = ConfigToModelHelper.School;

 

posted on 2018-08-02 11:18  高兴happy  阅读(480)  评论(0编辑  收藏  举报