2011-12-27 24 views
4

アプリケーションの構成ファイルからアイテムのコレクションを取得しようとしています。すべてがOKに見えますが、私は常に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

応答ありがとうございますが、それは動作しません。 –

+0

はい、私のせいで、正解を見つけましたが、すぐに投稿します –

+0

答えが更新されました。 –

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」には何も特別な意味がありません。さて、プロジェクトの設定で、タイプを自分のコレクションオブジェクトとして割り当てることができます。

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(); 

の問題です。ちょっとトリッキーなビットは、コレクションオブジェクトを宣言し、プロジェクトプロパティで設定を作成することです。

関連する問題