2011-12-27 99 views
4

我试图从应用程序的配置文件中获取项目的集合。一切看起来不错,但我总是取0元(不考虑,我把配置文件上...)从App.config文件读取集合

我的代码是:

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; 

任何想法??

在此先感谢

回答

3

你应该另一代码加载配置设置:

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

也确保assemply在您的配置文件中指定:

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

感谢您的回应,但它并没有工作。 – 2011-12-27 21:48:04

+0

是的,我的错,但是我找到了正确的答案,很快就会发布 – 2011-12-27 21:56:06

+0

答案已更新,请查看 – 2011-12-28 09:42:09

1

是否真的需要它是自己的配置部分?就我个人而言,我很少发现有必要超越项目属性中的简单设置。以下是我在一个项目中所做的工作,我希望使用允许和禁止的来源列表。我想保存在配置中的对象(在我的情况下,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”对此没有特别之处。现在,在项目的设置中,我可以将Type指定为我的集合对象。

setting the property to 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(); 

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

相关问题