System.Configuration.ConfigurationSection config配置对像
ConfigurationSection类主要是方便我们用于扩展自定义webcongfig中的节点信息。我们可以方便的通过以下方式获取【自定义节点对象】
1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3 4 <configSections> 5 6 <!--<section name="节点名称" type="程序集引用空间.类名称,程序集名称" />--> 7 <section name="RedisConfig" type="ConfigurationSectionText.RedisConfig,ConfigurationSection" /> 8 </configSections> 9 10 <RedisConfig Host="获取hoset" Port="端口号"> 11 </RedisConfig> 12 13 <startup> 14 <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" /> 15 </startup> 16 </configuration>
1 public sealed class RedisConfig : ConfigurationSection 2 { 3 public static RedisConfig GetConfig(string sectionName) 4 { 5 RedisConfig section = (RedisConfig)ConfigurationManager.GetSection(sectionName); 6 if (section == null) 7 throw new ConfigurationErrorsException("Section " + sectionName + " is not found."); 8 return section; 9 } 10 11 12 /// <summary> 13 /// 可写的Host链接地址 14 /// </summary> 15 [ConfigurationProperty("Host", IsRequired = false)] 16 public string Host 17 { 18 get 19 { 20 return (string)base["Host"]; 21 } 22 set 23 { 24 base["Host"] = value; 25 } 26 } 27 28 29 /// <summary> 30 /// 可写的Port端口号 31 /// </summary> 32 [ConfigurationProperty("Port", IsRequired = false)] 33 public string Port 34 { 35 get 36 { 37 return (string)base["Host"]; 38 } 39 set 40 { 41 base["Host"] = value; 42 } 43 } 44 45 }
浙公网安备 33010602011771号