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

これは機能していません。どうやら「プロパティ」構文を理解していないようです。

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

4

1 に答える 1

1

この方法で必要なプロパティを定義する場合:

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

各「プロパティ」が CustomFields の子ノードになり、最初の例のように CustomFields ノードの属性ではなく、プロパティがそれらの子ノードの属性/値になります。

多くのプロパティがあり、それらをよりエレガントに設定したい場合は、次の 2 つのオプションを検討してください。

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 の「方法: ConfigurationSection を使用してカスタム構成セクションを作成する」を参照してください。

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

于 2010-11-26T13:26:36.943 に答える