2010-11-25 124 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 setting1='hello' setting2='world'/> 

我這樣做:

<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; 
      } 
     } 
} 
} 

哪個不起作用。顯然它不理解'屬性'語法。

任何想法我做錯了什麼?謝謝。

+0

請問您可以發佈LoadValuesFromXml方法的代碼嗎?我認爲您的問題可能會阻礙您從XmlNode中檢索包含自定義部分信息的值。 – 2010-11-26 09:42:20

+0

我正在擴展配置設置。將添加該代碼。 – Damien 2010-11-26 10:41:26

回答

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; 
     } 
    } 
} 

Ť母雞,檢索值:

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

有關詳細信息,請參閱MSDN上的How to: Create Custom Configuration Sections Using ConfigurationSection

2)採用編程方法而不是依賴屬性和反射。 ConfigurationSection classIConfigurationSectionHandler可以在這種情況下使用。因此,您可以從代碼訪問包含自定義節數據的xml節點,並且可以加載任何類型的XML結構。