【发布时间】:2016-03-15 20:53:19
【问题描述】:
我正在尝试学习如何使用 ConfigurationSection 类。我曾经使用 IConfigurationSectionHandler,但发布它已被贬值。所以作为一个好孩子,我正在尝试“正确”的方式。我的问题是它总是返回 null。
我有一个控制台应用程序和一个 DLL。
class Program
{
static void Main(string[] args)
{
StandardConfigSectionHandler section = StandardConfigSectionHandler.GetConfiguration();
string value = section.Value;
}
}
应用配置:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="ConfigSectionGroup">
<section name="ConfigSection" type="Controller.StandardConfigSectionHandler, Controller" />
</sectionGroup>
</configSections>
<ConfigSectionGroup>
<ConfigSection>
<test value="1" />
</ConfigSection>
</ConfigSectionGroup>
</configuration>
DLL 中的节处理程序:
namespace Controller
{
public class StandardConfigSectionHandler : ConfigurationSection
{
private const string ConfigPath = "ConfigSectionGroup/ConfigSection/";
public static StandardConfigSectionHandler GetConfiguration()
{
object section = ConfigurationManager.GetSection(ConfigPath);
return section as StandardWcfConfigSectionHandler;
}
[ConfigurationProperty("value")]
public string Value
{
get { return (string)this["value"]; }
set { this["value"] = value; }
}
}
}
我为“ConfigPath”尝试的任何值都将返回 null,或者抛出一个错误,说“test”是一个无法识别的元素。我尝试过的值:
- 配置节组
- ConfigSectionGroup/
- ConfigSectionGroup/ConfigSection
- ConfigSectionGroup/ConfigSection/
- ConfigSectionGroup/ConfigSection/test
- ConfigSectionGroup/ConfigSection/test/
【问题讨论】:
标签: c# configurationmanager configurationsection