从 App.config 文件中读取集合

发布于 2024-12-22 20:34:13 字数 1751 浏览 0 评论 0原文

我正在尝试从应用程序的配置文件中获取项目集合。一切看起来都不错,但我总是获取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千寻… 2024-12-29 20:34:13

您应该使用另一个代码来加载配置设置:

TestConfigurationSection a = (TestConfigurationSection) System.Configuration.ConfigurationManager.GetSection("TestConfigurationSection");

还要确保在配置文件中指定了 assemply:

<section name="TestConfigurationSection" type="CustomSettingConfiguration.TestConfigurationSection, ConsoleApplication1" />

You should another code to load configuration settings:

TestConfigurationSection a = (TestConfigurationSection) System.Configuration.ConfigurationManager.GetSection("TestConfigurationSection");

also make sure that assemply is specified in your configuration file:

<section name="TestConfigurationSection" type="CustomSettingConfiguration.TestConfigurationSection, ConsoleApplication1" />
梦太阳 2024-12-29 20:34:13

它真的需要它自己的配置部分吗?就我个人而言,我很少发现有必要超出项目属性中的简单设置。以下是我在一个项目中的做法,我想使用允许和禁止的源列表。我想在配置中保存的对象(在我的例子中是 user.config,但原理与 app.config 相同)是一个普通的 c# 对象(它实现了一个与本次讨论无关的接口)。

因此,为了简单起见,我为我的对象创建了一个集合类。这简化了设置部分。这是该类的完整内容:

// This is mainly declared to ease use as a User Setting
public class SpellSourceCollection : List<SpellSource>
{
    public SpellSourceCollection() : base() { }
    public SpellSourceCollection(IEnumerable<SpellSource> ListToCopy)
        : this()
    {
        this.AddRange(ListToCopy);
    }
}

请记住,“SpellSource”没有什么特别之处。现在,在项目的设置中,我可以将类型指定为我的集合对象。

将属性设置为 SpellSourceCollection

您可能必须“浏览”到正确的自定义对象。然而,一旦完成,从 app.config(或 user.config)读取就变得轻而易举。配置文件如下所示(略有缩写)。

<setting name="Sources" serializeAs="Xml">
    <value>
        <ArrayOfSpellSource xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <SpellSource>
                <Source>PFRPG Advanced Player's Guide</Source>
                <Allowed>true</Allowed>
                <BackgroundColor>White</BackgroundColor>
            </SpellSource>
            <SpellSource>
                <Source>PFRPG Core</Source>
                <Allowed>true</Allowed>
                <BackgroundColor>White</BackgroundColor>
            </SpellSource>
            <SpellSource>
                <Source>Rival Guide</Source>
                <Allowed>false</Allowed>
                <BackgroundColor>White</BackgroundColor>
            </SpellSource>
            <SpellSource>
                <Source>Ultimate Combat</Source>
                <Allowed>true</Allowed>
                <BackgroundColor>White</BackgroundColor>
            </SpellSource>
            <SpellSource>
                <Source>Ultimate Magic</Source>
                <Allowed>true</Allowed>
                <BackgroundColor>Cyan</BackgroundColor>
            </SpellSource>
        </ArrayOfSpellSource>
    </value>
</setting>

获得该房产只是一个简单的问题,

SpellSourceCollection settingsSources = Properties.Settings.Default.Sources;
// do stuff or even later in your project, you can save this user setting
Properties.Settings.Default.Sources = settingsSources;
Properties.Settings.Default.Save();

您可以以类似的方式将其应用到您自己的项目中。唯一稍微棘手的部分是声明集合对象并在项目属性中创建设置。

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:

// This is mainly declared to ease use as a User Setting
public class SpellSourceCollection : List<SpellSource>
{
    public SpellSourceCollection() : base() { }
    public SpellSourceCollection(IEnumerable<SpellSource> ListToCopy)
        : this()
    {
        this.AddRange(ListToCopy);
    }
}

Remember that "SpellSource" has nothing special about it. Now, in the settings for the project, I can assign the Type as my collection object.

setting the property to SpellSourceCollection

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).

<setting name="Sources" serializeAs="Xml">
    <value>
        <ArrayOfSpellSource xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
            <SpellSource>
                <Source>PFRPG Advanced Player's Guide</Source>
                <Allowed>true</Allowed>
                <BackgroundColor>White</BackgroundColor>
            </SpellSource>
            <SpellSource>
                <Source>PFRPG Core</Source>
                <Allowed>true</Allowed>
                <BackgroundColor>White</BackgroundColor>
            </SpellSource>
            <SpellSource>
                <Source>Rival Guide</Source>
                <Allowed>false</Allowed>
                <BackgroundColor>White</BackgroundColor>
            </SpellSource>
            <SpellSource>
                <Source>Ultimate Combat</Source>
                <Allowed>true</Allowed>
                <BackgroundColor>White</BackgroundColor>
            </SpellSource>
            <SpellSource>
                <Source>Ultimate Magic</Source>
                <Allowed>true</Allowed>
                <BackgroundColor>Cyan</BackgroundColor>
            </SpellSource>
        </ArrayOfSpellSource>
    </value>
</setting>

Getting at the property is simply a matter of

SpellSourceCollection settingsSources = Properties.Settings.Default.Sources;
// do stuff or even later in your project, you can save this user setting
Properties.Settings.Default.Sources = settingsSources;
Properties.Settings.Default.Save();

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文