C# 獲取config文件 實體轉換

隨着項目的擴展,單獨的key,value配置文件已經不能知足需求了app

這裏須要自定義配置節點,例如ui

1   <!--自定義  具體實體類配置問節點信息-->
2   <School Name="紅旗小學" Number="1008" Address="北京市,西城區……"></School>

固然,這樣的節點能夠有多重獲取方式,講本身認爲比較好的方式總結一下,僅供參考this

能夠將當前節點當作試題Model來配置,這樣方便管理和操做,下面說明下如何進行轉換spa

一、首先定義實體,由於須要轉換,這裏用到了configSections的section 節點,配置實體的命名空間code

而後試題還須要繼承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 }

二、理由ExeConfigurationFileMap類將自定義配置文件轉換爲Configuration類blog

而後從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

完整幫助類代碼以下get

 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     }

調用:string

1  School school = ConfigToModelHelper.School;
相關文章
相關標籤/搜索