2010-11-25 16 views
0

Windowsフォームアプリケーションのapp.configファイルにカスタムセクションを追加しました。カスタム設定の問題

CustomFields myCustomFields = (CustomFields)System.Configuration.ConfigurationManager.GetSection("CustomFields"); 

私はセクション名を指定します:私は問題だと思うところ今ここ

<section name="CustomFields" type="Application.Core.CustomFields, ATMCardRequest.Core" allowLocation="true" allowDefinition="Everywhere" /> 

がある私は、構成ファイルを拡張するクラスを作成しました。上記の前にうまく働いたが、私は、このセクションのため、代わりにこれを行うの性質の多くを必要としています

<CustomFields> 
<property name="setting1">hello</property> 
<property name="setting2">world</property> 
... 
</CustomFields> 

コード:

/// <summary> 
    /// Settings file which holds the name of the XML Fields 
    /// </summary> 
    public class setting1: ConfigurationSection 
    { 

     /// <summary> 
     /// Name of the setting1 Field 
     /// </summary> 
     [ConfigurationProperty("setting1", IsRequired = true)] 
     public String setting1 
     { 
      get 
      { 
       return (String)this["setting1"]; 
      } 
      set 
      { 
       this["setting1"] = value; 
      } 
     } 

     /// <summary> 
     /// Name of the setting2 Field 
     /// </summary> 
     [ConfigurationProperty("setting2",IsRequired = true)] 
     public String setting2 
     { 
      get 
      { 
       return (String)this["setting2"]; 
      } 
      set 
      { 
       this["setting2"] = value; 
      } 
     } 
} 
} 

私はこれをやっている

<CustomFields setting1='hello' setting2='world'/> 

どちらがうまくいかない。どうやらそれは 'プロパティ'の構文を理解していません。

私が間違っていることは何ですか?ありがとう。

+0

LoadValuesFromXmlメソッドのコードを投稿してください。あなたの問題は、カスタムセクションの情報が含まれているXmlNodeから値を取得しているように見えるかもしれません。 –

+0

設定を拡張しています。そのコードを追加します。 – Damien

答えて

1

あなたがこの方法を必要とするプロパティを定義する場合は、次の各「プロパティ」より

<CustomFields> 
    <property name="setting1">hello</property> 
    <property name="setting2">world</property> 
    ... 
</CustomFields> 

はCustomFieldsとあなたの特性のための子ノードになりは、現在の属性です/それらの子ノードではなく、CustomFieldsの属性の値を最初の例のようにノード。

あなたがプロパティの多くを持っていて、ここよりエレガントにそれらを設定したい場合は検討するかもしれない二つのオプションです:

1)は、カスタムセクション(若干変更)については、以下の構造を使用します

<CustomFields> 
    <setting1 value="hello"/> 
    <setting2 value="world"/> 
    ... 
</CustomFields> 
と値を取得するために使用されるプロパティを定義するために、次のコード:

public class CustomFields: ConfigurationSection 
{    
    [ConfigurationProperty("setting1")] 
    public PropertyElement Setting1 
    { 
     get 
     { 
      return (PropertyElement)this["setting1"]; 
     } 
     set 
      { this["setting1"] = value; } 
    } 

    [ConfigurationProperty("setting2")] 
    public PropertyElement Setting2 
    { 
     get 
     { 
      return (PropertyElement)this["setting2"]; 
     } 
     set 
      { this["setting2"] = value; } 
    } 
} 
public class PropertyElement : ConfigurationElement 
{ 
    [ConfigurationProperty("value", IsRequired = false)] 
    public String Value 
    { 
     get 
     { 
      return (String)this["value"]; 
     } 
     set 
     { 
      this["value"] = value; 
     } 
    } 
} 

T鶏は、値を取得するには:詳細については

string setting1value = myCustomFields.Setting1.Value; 
string setting2value = myCustomFields.Setting2.Value; 

は、MSDNにHow to: Create Custom Configuration Sections Using ConfigurationSectionを参照してください。

2)属性とリフレクションに頼るのではなく、プログラムでアプローチします。この場合、ConfigurationSection classまたはIConfigurationSectionHandlerを使用できます。その結果、コードからカスタムセクションデータを含むxmlノードにアクセスし、あらゆる種類のXML構造をロードできます。

+0

乾杯!それが私が探していたものです。 – Damien

+0

あなたは大歓迎です:) –