Web.config或App.config下section

1.为什么使用section

在web.config中不能直接使用用户自定义的配置信息,但定义配置节处理程序与配置元素之间的关联来获取配置信息,具体查看msdn。

如log4net,NHibernate等很多程序集,在使用时,用户自己配置section信息,程序集通过配置信息来处理逻辑,增加应用程序的可移植性。

2.注意

<?xml version="1.0"?>
<configuration>
  <configSections>
    <sectionGroup name="myCustomGroup">
      <section name="myCustomSection" type="ExampleSectionHandler.Handler.MyHandler,ExampleSectionHandler"/>
    </sectionGroup>
  </configSections>
  <myCustomGroup>
    <myCustomSection myAttrib1="Clowns">
      <myChildSection myChildAttrib1="Zippy" myChildAttrib2="Michael Zawondy "/>
    </myCustomSection>
  </myCustomGroup>
  <system.web>
  </system.web>
</configuration>

System.Configuration.ConfigurationSettings.GetConfig("myCustomGroup/myCustomSection")返回object

程序中,通过GetConfig来查找对应的sectionGroup和section,执行type对应的节点处理程序,它返回Object类型(任意数据结构)

3.疑问

访问某一节,可使用GetConfig(string sectionName),如何遍历所有Section呢

原文地址:https://www.cnblogs.com/goahead777/p/2784422.html