从 App.config 文件中读取集合
我正在尝试从应用程序的配置文件中获取项目集合。一切看起来都不错,但我总是获取 0 个元素(不管我是否放入配置文件...)
我的代码是:
using System.Configuration;
namespace CustomSettingConfiguration
{
public class TestConfigurationElement : ConfigurationElement
{
[ConfigurationProperty("name", IsKey = true, IsRequired = true)]
public string Name
{
get { return (string) this["name"]; }
}
}
[ConfigurationCollection(typeof (TestConfigurationElement), AddItemName = "test")]
public class TestConfigurationElementCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new TestConfigurationElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((TestConfigurationElement) element).Name;
}
}
public class TestConfigurationSection : ConfigurationSection
{
[ConfigurationProperty("Tests", IsDefaultCollection = true)]
public TestConfigurationElementCollection Tests
{
get { return (TestConfigurationElementCollection)this["Tests"]; }
}
}
}
配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="TestConfigurationSection" type="CustomSettingConfiguration.TestConfigurationSection" />
</configSections>
<TestConfigurationSection>
<Tests>
<test name="One" />
<test name="Two" />
</Tests>
</TestConfigurationSection>
</configuration>
使用它:
TestConfigurationSection a = new TestConfigurationSection();
var tests = a.Tests;
知道吗?
提前致谢
I'm trying to get a collection of Items from the configuration file of an application. Everything looks ok, but I always fetch 0 elements (regardless that I put on the configuration file...)
My code is:
using System.Configuration;
namespace CustomSettingConfiguration
{
public class TestConfigurationElement : ConfigurationElement
{
[ConfigurationProperty("name", IsKey = true, IsRequired = true)]
public string Name
{
get { return (string) this["name"]; }
}
}
[ConfigurationCollection(typeof (TestConfigurationElement), AddItemName = "test")]
public class TestConfigurationElementCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new TestConfigurationElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((TestConfigurationElement) element).Name;
}
}
public class TestConfigurationSection : ConfigurationSection
{
[ConfigurationProperty("Tests", IsDefaultCollection = true)]
public TestConfigurationElementCollection Tests
{
get { return (TestConfigurationElementCollection)this["Tests"]; }
}
}
}
And the configuration file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="TestConfigurationSection" type="CustomSettingConfiguration.TestConfigurationSection" />
</configSections>
<TestConfigurationSection>
<Tests>
<test name="One" />
<test name="Two" />
</Tests>
</TestConfigurationSection>
</configuration>
To use It:
TestConfigurationSection a = new TestConfigurationSection();
var tests = a.Tests;
Any idea??
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该使用另一个代码来加载配置设置:
还要确保在配置文件中指定了 assemply:
You should another code to load configuration settings:
also make sure that assemply is specified in your configuration file:
它真的需要它自己的配置部分吗?就我个人而言,我很少发现有必要超出项目属性中的简单设置。以下是我在一个项目中的做法,我想使用允许和禁止的源列表。我想在配置中保存的对象(在我的例子中是 user.config,但原理与 app.config 相同)是一个普通的 c# 对象(它实现了一个与本次讨论无关的接口)。
因此,为了简单起见,我为我的对象创建了一个集合类。这简化了设置部分。这是该类的完整内容:
请记住,“SpellSource”没有什么特别之处。现在,在项目的设置中,我可以将类型指定为我的集合对象。
您可能必须“浏览”到正确的自定义对象。然而,一旦完成,从 app.config(或 user.config)读取就变得轻而易举。配置文件如下所示(略有缩写)。
获得该房产只是一个简单的问题,
您可以以类似的方式将其应用到您自己的项目中。唯一稍微棘手的部分是声明集合对象并在项目属性中创建设置。
Does it really need it's own configuration section? Personally, I seldom find it necessary to go beyond the simple settings in the project properties. Here's how I did it in a project where I wanted to use a list of sources that were allowed and disallowed. The object I wanted to save in configuration (in my case, user.config, but the principle is the same for app.config) is a plain c# object (it implements an interface that isn't germane to this discussion is all).
So to make it easy, I created a collection class for my object. This simplifies the setting-up part. Here's the class, in its entirety:
Remember that "SpellSource" has nothing special about it. Now, in the settings for the project, I can assign the Type as my collection object.
You may have to "Browse" to the correct custom object. Once it's done, however, reading from app.config (or user.config) is a breeze. Here's what the config file looks like (slightly abbreviated).
Getting at the property is simply a matter of
You can apply that to your own project in similar fashion. The only mildly tricky bits are declaring the collection object and creating the setting in the project properties.