2009-02-20 4 views
13

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


//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

私は私のトップオフを考えることができる2つのことは、次のようにDefaultValueを使用することになります。

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

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

第二の考えではなく、NULL可能int型を使用することです:何も設定に設定されていない場合

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

さて、それはこれまでのところ、私はできていませんでした代わりに0

+0

これは機能しますが、プロパティが定義されていないときにデフォルトを抑制する方法があることを期待していました。私が今使っている作業はconfig.Setting2.IsPresent – user62064

0

のデフォルトでnullにすべきですプロパティーが構成ファイルに定義されていない場合は、プロパティーにヌルであることを通知します。それは無限の知恵で、Microsoftはあなたがnullを入力すると、あなたが本当にString.Emptyまたは新しい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; } 
    } 

し、必要に応じて誤ってプロパティをマークします。あなたが設定ファイルを編集するためにツールを使用しているなら、これは好きではありません。

11

ConfigurationSection.PostDeserialize()をオーバーライドし、ConfigurationElementから派生した各セクションメンバーのIsPresentプロパティを確認するのが最善の方法です。

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されます。

+0

です。ちなみに、 'PostDeserialize'イベントでこれを行う必要はありません。 'ElementInformation'は常に利用可能です:' Console.WriteLine( "Setting1 {0}"、config.Setting1.CustomSettingItem.ElementInformation.IsPresent? "Y": "N"); ' –

2

は、以下のことを試してみてください。

configElement.ElementInformation.Properties[propName].ValueOrigin = 
     PropertyValueOrigin.SetHere 

値から来ないところValueOriginプロパティがわかります。

+0

' IsPresent'が期待通りに戻っていませんでした。何らかの理由で値が(それはすべての場合にfalseを返しました)。「ValueOrigin」が私のために働いた。 – atheaos

0

また、次の使用してチェックすることができます。それはあなたの設定ファイルで見つからなかった場合

config.Setting1.CustomSettingItem.ElementInformation.IsPresent 

それはあなたが偽与えるだろう。

関連する問題