2009-02-20 22 views
13

मैं पिछले 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

2 बातें मैं के ऊपर से सोच सकते हैं मेरी सिर एक डिफ़ॉल्ट वैल्यू का उपयोग करना होगा, जैसे:

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

मान लीजिए कि कुछ मूल्य अमान्य है। इस मामले में, CustomSettingItem == -1 का अर्थ है कि यह सेट नहीं किया गया था, और> = 0 कॉन्फ़िगरेशन में एक मान सेट था। बेशक यह मानता है कि पहले स्थान पर वैध इनपुट नहीं था।

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

अब अगर कुछ भी नहीं config में सेट कर दिया जाता है, यह 0.

+0

यह काम करता है, लेकिन मुझे उम्मीद थी कि संपत्ति को परिभाषित नहीं होने पर डिफ़ॉल्ट को दबाने का कोई तरीका था। मैं जिस काम का उपयोग कर रहा हूं वह अभी कॉन्फ़िगर कर रहा है config.Setting2.IsPresent – user62064

0

के बजाय शून्य पर अब तक मैं सक्षम नहीं किया है किया गया डिफ़ॉल्ट होना चाहिए:

दूसरा विचार के बजाय एक नल पूर्णांक उपयोग करने के लिए है किसी संपत्ति को शून्य होने के बारे में बताने के लिए यदि यह कॉन्फ़िगरेशन फ़ाइल में परिभाषित नहीं है। ऐसा लगता है कि इसके अनंत ज्ञान में, माइक्रोसॉफ्ट ने तय किया कि आप वास्तव में स्ट्रिंग का अनुभव करते हैं। लक्षण या नया कॉन्फ़िगरेशन एलिमेंट() जब आप शून्य टाइप करते हैं।

तरह से मैं वर्तमान में इसे सुलझाने कर रहा हूँ इस तरह है:

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 कि config फ़ाइल से पढ़ने नहीं किया गया है null बाद में किया जाएगा।

+0

एक साइड नोट के रूप में, आपको इसे 'PostDeserialize' ईवेंट पर नहीं करना है। 'ElementInformation' हमेशा उपलब्ध है: 'Console.WriteLine (" Setting1 {0} ", config.Setting1.CustomSettingItem.ElementInformation.IsPresent?" वाई ":" एन ");' –

2

निम्नलिखित का प्रयास करें:

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

ValueOrigin संपत्ति आपको बताता है जहां मूल्य से आता है।

+0

'IsPresent' अपेक्षित वापस नहीं कर रहा था किसी कारण के लिए मूल्य (यह सब कुछ के लिए झूठा लौटा)।'ValueOrigin' मेरे लिए काम किया। – atheaos

0

आप निम्न का उपयोग कर सकते हैं:

config.Setting1.CustomSettingItem.ElementInformation.IsPresent 

यह आप गलत अगर यह आपके कॉन्फ़िग फ़ाइल में नहीं मिला था दे देंगे।

संबंधित मुद्दे