में संग्रह के साथ सरल कस्टम कॉन्फ़िगरेशन अनुभाग। मैं .NET4 अनुप्रयोग के लिए एक बहुत ही सरल कस्टम कॉन्फ़िगरेशन अनुभाग लिखने की कोशिश कर रहा हूं।.NET4
<configuration>
<configSections>
<section name="myServices" type="My.ConfigSection, My.Assembly" />
</configSections>
<myServices>
<add name="First" />
<add name="Second" />
</myServices>
</configuration>
हालांकि, मैं एक ConfigurationErrorsException
मिलती रहती है: मेरा लक्ष्य यह है 'न पहचाना गया तत्व' जोड़ें '' जब मैं ConfigurationManager.GetSection("myServices")
कहते हैं। मैं थोड़ी देर के लिए इस पर घूर रहा हूं लेकिन अभी तक यह पता नहीं लगाया है कि मैं क्या गलत कर रहा हूं। नीचे मेरा कोड है। यह तीन वर्ग है: ConfigSection
, MyServiceSettingsCollection
और MyServiceSettings
।
पहले कक्षा जो संपूर्ण कॉन्फ़िगरेशन अनुभाग का प्रतिनिधित्व करती है। इसमें MyServiceSettingsCollection
का नामहीन डिफ़ॉल्ट संग्रह है। IsDefaultCollection
संपत्ति मुझे रूट तत्व से सीधे मेरे संग्रह में 'जोड़ने' की अनुमति देनी चाहिए।
public sealed class ConfigSection : ConfigurationSection
{
private static readonly ConfigurationProperty _propMyServices;
private static readonly ConfigurationPropertyCollection _properties;
public static ConfigSection Instance { get { return _instance; } }
static ConfigSection()
{
_propMyServices = new ConfigurationProperty(
null, typeof(MyServiceSettingsCollection), null,
ConfigurationPropertyOptions.IsDefaultCollection);
_properties = new ConfigurationPropertyCollection { _propMyServices };
}
[ConfigurationProperty("", IsDefaultCollection = true)]
public MyServiceSettingsCollection MyServices
{
get { return (MyServiceSettingsCollection) base[_propMyServices]; }
set { base[_propMyServices] = value; }
}
protected override ConfigurationPropertyCollection Properties
{ get { return _properties; } }
}
अगला, संग्रह वर्ग स्वयं। यह AddRemoveClearMap
प्रकार है।
[ConfigurationCollection(typeof(MyServiceSettings),
CollectionType = ConfigurationElementCollectionType.AddRemoveClearMap)]
public sealed class MyServiceSettingsCollection : ConfigurationElementCollection
{
public MyServiceSettings this[int index]
{
get { return (MyServiceSettings) BaseGet(index); }
set
{
if (BaseGet(index) != null) { BaseRemoveAt(index); }
BaseAdd(index, value);
}
}
public new MyServiceSettings this[string key]
{
get { return (MyServiceSettings) BaseGet(key); }
}
protected override ConfigurationElement CreateNewElement()
{
return new MyServiceSettings();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((MyServiceSettings) element).Key;
}
}
और अंत में संग्रह में तत्वों के लिए एक कक्षा। अभी के लिए, इस वर्ग में एक संपत्ति है लेकिन बाद में और अधिक होगा (जो मुझे NameValueSectionHandler
का उपयोग करने से रोकता है)।
public class MyServiceSettings : ConfigurationElement
{
private static readonly ConfigurationProperty _propName;
private static readonly ConfigurationPropertyCollection properties;
static MyServiceSettings()
{
_propName = new ConfigurationProperty("name", typeof(string), null, null,
new StringValidator(1),
ConfigurationPropertyOptions.IsRequired |
ConfigurationPropertyOptions.IsKey);
properties = new ConfigurationPropertyCollection { _propName };
}
[ConfigurationProperty("name", DefaultValue = "",
Options = ConfigurationPropertyOptions.IsRequired |
ConfigurationPropertyOptions.IsKey)]
public string Name
{
get { return (string) base[_propKey]; }
set { base[_propKey] = value; }
}
protected override ConfigurationPropertyCollection Properties
{ get { return properties; } }
}
लेकिन यह जानना अच्छा है कि यह कैसे काम करता है - अगले सप्ताह में कुछ हासिल करने के समान होगा, इसलिए धन्यवाद :-) – Yahia
यह अजीब बात है। दोनों 'IsDefaultCollection' और' विकल्प 'मेरे लिए काम करते हैं। लेकिन संपत्ति के नाम के लिए खाली स्ट्रिंग रखना अनिवार्य प्रतीत होता है। –
ConfigurationPropertyAttribute.cs के संदर्भ स्रोत को देखते हुए, IsDefaultCollection प्रॉपर्टी विकल्प फ़्लैग प्रॉपर्टी पर केवल एक रैपर है - http://referencesource.microsoft.com/#System.Configuration/System/Configuration/ConfigurationPropertyAttribute.cs,62 –