技术开发 频道

揭开.NET 2.0配置隐藏的秘密

private string m_string;

  
private bool m_bool;

  
private TimeSpan m_timespan;

  void GetExampleSettings()

  {

  ExampleSection section
= ConfigurationManager.GetSection("example")

  
as ExampleSection;

  
if (section != null)

  {

  m_string
= section.StringValue;

  m_bool
= section.BooleanValue;

  m_timespan
= section.TimeSpanValue;

  }

  }

这里最重要注意的是选择代表一个配置节根元素的名字。在App.config文件的例子中,节被定义为 “example”。因为调用GetSection()时加载节查找名字‘example’,在App.config中任何试图为“example“重命名为其他名字将导致GetExampleSettings()失败。这是不可以任意选择一个自定义配置节的名子,除非明确地设计调整以保持一致,可能是通过使用另一个配置节。

0
相关文章