当尝试访问 ConnectionConfiguration
时,对于整数数据类型总是返回默认值或 0
。不会抛出任何错误,它只是无法获取每个属性的值。请帮我解决这个问题。
我在控制台应用程序中有一个 app.config 文件,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="templates" >
<section name="connections" type="Test.ConnectionConfiguration, Test" allowLocation="true" allowDefinition="Everywhere"/>
</sectionGroup>
</configSections>
<templates>
<connections allowed="true" port="8080">
<internal name="local" size="1344"/>
<external name="internet" value="http" />
</connections>
</templates>
</configuration>
类
public class ConnectionConfiguration : ConfigurationSection
{
[ConfigurationProperty("allowed", IsRequired = true)]
public bool Allowed
{
get
{
return (bool)this["allowed"];
}
set
{
this["allowed"] = value;
}
}
[ConfigurationProperty("port", IsRequired = true)]
public int Port
{
get { return (int)this["port"]; }
set { this["port"] = value; }
}
[ConfigurationProperty("internal")]
public InternalElement Internal
{
get { return (InternalElement)this["internal"]; }
set { this["internal"] = value; }
}
[ConfigurationProperty("external")]
public ExternalElement External
{
get { return (ExternalElement)this["external"]; }
set { this["external"] = value; }
}
}
public class InternalElement : ConfigurationElement
{
[ConfigurationProperty("name", DefaultValue = "local", IsRequired = true)]
[StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 60)]
public string Name
{
get { return (string)this["name"]; }
set { this["name"] = value; }
}
[ConfigurationProperty("size", DefaultValue = "1234", IsRequired = false)]
[IntegerValidator(ExcludeRange = false, MaxValue = 6000, MinValue = 1)]
public int Size
{
get { return (int)this["size"]; }
set { this["size"] = value; }
}
}
public class ExternalElement : ConfigurationElement
{
[ConfigurationProperty("name", DefaultValue = "local", IsRequired = true)]
[StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'\"|\\", MinLength = 1, MaxLength = 60)]
public string Name
{
get { return (string)this["name"]; }
set { this["name"] = value; }
}
[ConfigurationProperty("value", DefaultValue = "https", IsRequired = false)]
public string Value
{
get { return (string)this["value"]; }
set { this["value"] = value; }
}
}
提前致谢!
ConnectionConfiguration config = new ConnectionConfiguration();
int test = config.Port; // it should be 8080 but it's 0
最佳答案
不幸的是,仅仅创建配置处理程序的新实例并不会自动用配置数据填充它。你必须手动阅读。为了实现这一点,您可以在 ConnectionConfiguration 类中添加一个静态属性,例如:
public class ConnectionConfiguration : ConfigurationSection
{
private static ConnectionConfiguration connections = ConfigurationManager.GetSection("templates/connections") as ConnectionConfiguration;
public static ConnectionConfiguration Connections
{
get
{
return connections;
}
}
// the rest is the same
}
在您的应用程序中,您现在可以像这样访问自定义配置部分:
ConnectionConfiguration config = ConnectionConfiguration.Connections;
int test = config.Port; // it should now be 8080
关于c# - 带有 sectionGroup 的自定义 ConfigurationSection 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33105743/