简体   繁体   English

从App.config文件中读取集合

[英]Reading a collection from the App.config file

I'm trying to get a collection of Items from the configuration file of an application. 我正在尝试从应用程序的配置文件中获取Items集合。 Everything looks ok, but I always fetch 0 elements (regardless that I put on the configuration file...) 一切看起来都不错,但我总是拿0个元素(不管我放在配置文件上......)

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 提前致谢

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: 还要确保在配置文件中指定了assemply:

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

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). 我想在配置中保存的对象(在我的例子中,user.config,但app.config的原理是相同的)是一个简单的c#对象(它实现了一个与此讨论没有密切关系的接口)。

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. 请记住,“SpellSource”没有什么特别之处。 Now, in the settings for the project, I can assign the Type as my collection object. 现在,在项目的设置中,我可以将Type指定为我的集合对象。

将属性设置为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. 但是,一旦完成,从app.config(或user.config)读取是一件轻而易举的事。 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. 唯一有点棘手的问题是声明集合对象并在项目属性中创建设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM