14

私は過去3日間ネットを精査してきましたが、この質問への参照が見つかりません。app.configで使用するカスタム構成クラスを作成しました。すべてが正常に動作します。この問題は、(構成要素の)構成プロパティが不要であり、app.configで定義されていない場合に発生します。構成プロパティにはデフォルト値が返されているようです。プロパティがapp.configで定義されていないかどうかを判断する方法を知っている人はいますか?(app.configを投稿しようとしていますが、その方法がわかりません...誰か知っていますか?)


//Main
namespace TestStub
{
    class Program
    {
        static void Main(string[] args)
        {
            CustomSettingsHandler config = (CustomSettingsHandler)ConfigurationManager.GetSection("CustomSettingsManager");
            Console.WriteLine("Setting1 {0}", config.Setting1.CustomSettingItem);
            Console.WriteLine("Setting2 {0}", config.Setting2.CustomSettingItem);
        }
    }
}

//Custom Configuration Class
namespace CustomConfiguration
{
    public class CustomSettingsHandler : ConfigurationSection
    {
        [ConfigurationProperty("setting1", IsRequired = false)]
        public CustomSettingElement Setting1 { get { return (CustomSettingElement)this["setting1"]; } }

        [ConfigurationProperty("setting2", IsRequired = false)]
        public CustomSettingElement Setting2 { get { return (CustomSettingElement)this["setting2"]; } }
    }

    public class CustomSettingElement : ConfigurationElement
    {
        [ConfigurationProperty("customsettingitem", IsRequired = false)]
        public int CustomSettingItem { get { return (int)this["customsettingitem"]; } }
    }
}
4

5 に答える 5

11

から派生した各セクション メンバーConfigurationSection.PostDeserialize()のプロパティをオーバーライドしてチェックするのが最善の方法であることがわかりました。IsPresentConfigurationElement

public class CustomSettingsHandler : ConfigurationSection
{
    // ...

    protected override void PostDeserialize()
    {
        foreach (ConfigurationProperty property in Properties)
        {
            var configElement = this[property] as ConfigurationElement;

            if (configElement != null 
                && !configElement.ElementInformation.IsPresent)
            {
                this[property] = null;
            }
        }

        base.PostDeserialize();
    }
}

ConfigurationElement設定ファイルから読み込めていないものはそれぞれnull後になります。

于 2013-05-13T13:45:16.407 に答える
4

私が頭の中で考えることができる2つのことは、次のようにDefaultValueを使用することです。

    [ConfigurationProperty("customsettingitem", DefaultValue = -1)]
    public int CustomSettingItem { get { return (int)this["customsettingitem"]; } }

無効な値があると仮定します。この場合、CustomSettingItem == -1は設定されていないことを意味し、>=0はconfigで設定された値でした。もちろん、そもそも-1が有効な入力ではなかったと仮定します。

2番目のアイデアは、代わりにnull許容整数を使用することです。

    [ConfigurationProperty("customsettingitem", IsRequired = false)]
    public int? CustomSettingItem { get { return (int?)this["customsettingitem"]; } }

これで、configに何も設定されていない場合、デフォルトで0ではなくnullになります。

于 2009-02-20T03:10:42.140 に答える
0

これまでのところ、構成ファイルで定義されていないプロパティを null にすることはできませんでした。無限の知恵の中で、Microsoft は、null を入力したときに本当に String.Empty または new ConfigurationElement() を意味すると判断したようです。

私が現在それを解決している方法は次のようなものです:

    bool _hasProp = true;
    protected override object OnRequiredPropertyNotFound(string name)
    {
        if (name == "prop")
        {
            _hasProp = false;
            return null; // note that this will still not make prop null
        }
        return base.OnRequiredPropertyNotFound(name);
    }

    [ConfigurationProperty("prop", IsRequired = true)]
    public string Prop
    {
        get { return _hasProp ? (string) this["prop"] : null; }
    }

これはハックであり、プロパティを誤って必要としてマークします。ツールを使用して構成ファイルを編集している場合、これは気に入らないでしょう。

于 2011-01-06T14:35:40.053 に答える
0

以下を使用して確認することもできます。

config.Setting1.CustomSettingItem.ElementInformation.IsPresent 

設定ファイルに見つからなかった場合は false になります。

于 2016-05-26T22:36:22.007 に答える