तो MyApp.PluginsConfiguration एक ConfigurationSection है, तो आप एक नया वर्ग है कि ConfigurationElementCollection से विरासत को परिभाषित करने और के बारे में कुछ में गहराई से जानकारी के लिए नया वर्ग MyApp.PluginsConfiguration
चेक this article के ConfigurationProperty कर सकते हैं उन प्रकारों। मैं भी blogged about नेस्टेड गुण है, लेकिन विशेष रूप से नहीं संग्रहों के लिए।
संपादित करें: कुछ कोड के साथ यहाँ। यह वेब.config में दिया गया है:
<configSections>
<section name="plugins"
type="WebApplication1.PluginsConfiguration, WebApplication1"/>
</configSections>
<plugins>
<use assembly="MyApp.Plugin1.dll"/>
<use assembly="MyApp.Plugin2.dll"/>
</plugins>
ऐसा करने के लिए कक्षाएं यहां दी गई हैं। ध्यान दें कि शायद NullReferenceExceptions जिन्हें संभालने की आवश्यकता होगी।
namespace WebApplication1
{
public class PluginsConfiguration : ConfigurationSection
{
private static ConfigurationPropertyCollection properties;
private static ConfigurationProperty propPlugins;
static PluginsConfiguration()
{
propPlugins = new ConfigurationProperty(null, typeof(PluginsElementCollection),
null,
ConfigurationPropertyOptions.IsDefaultCollection);
properties = new ConfigurationPropertyCollection { propPlugins };
}
protected override ConfigurationPropertyCollection Properties
{
get
{
return properties;
}
}
public PluginsElementCollection Plugins
{
get
{
return this[propPlugins] as PluginsElementCollection;
}
}
}
public class PluginsElementCollection : ConfigurationElementCollection
{
public PluginsElementCollection()
{
properties = new ConfigurationPropertyCollection();
}
private static ConfigurationPropertyCollection properties;
protected override ConfigurationPropertyCollection Properties
{
get
{
return properties;
}
}
public override ConfigurationElementCollectionType CollectionType
{
get
{
return ConfigurationElementCollectionType.BasicMap;
}
}
protected override string ElementName
{
get
{
return "use";
}
}
protected override ConfigurationElement CreateNewElement()
{
return new PluginsElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
var elm = element as PluginsElement;
if (elm == null) throw new ArgumentNullException();
return elm.AssemblyName;
}
}
public class PluginsElement : ConfigurationElement
{
private static ConfigurationPropertyCollection properties;
private static ConfigurationProperty propAssembly;
protected override ConfigurationPropertyCollection Properties
{
get
{
return properties;
}
}
public PluginsElement()
{
propAssembly = new ConfigurationProperty("assembly", typeof(string),
null,
ConfigurationPropertyOptions.IsKey);
properties = new ConfigurationPropertyCollection { propAssembly };
}
public PluginsElement(string assemblyName)
: this()
{
AssemblyName = assemblyName;
}
public string AssemblyName
{
get
{
return this[propAssembly] as string;
}
set
{
this[propAssembly] = value;
}
}
}
}
और यह तक पहुँचने के लिए, इस कोड स्निपेट की मदद करनी चाहिए:
var cfg = WebConfigurationManager.GetWebApplicationSection("plugins") as PluginsConfiguration;
var sb = new StringBuilder();
foreach(PluginsElement elem in cfg.Plugins)
{
sb.AppendFormat("{0}<br/>", elem.AssemblyName);
}
testLabel.Text = sb.ToString();
असल में हम एक ConfigurationSection कि प्लगइन्स तत्व संभालती है। इस में, हम एक ConfigurationElementCollection संपत्ति निर्दिष्ट और डिफ़ॉल्ट संग्रह (आप सिद्धांत में कर सकता है एक रूट नोड के अंतर्गत कई विभिन्न संग्रह है) के रूप में यह घोषणा करते हैं।
PluginsElementCollection ConfigurationElementCollection लागू करता है। हमारे मामले में "उपयोग" में एलिमेंटनाम को टैग का नाम होना चाहिए। साथ ही, GetElementKey को ओवरराइड करने की आवश्यकता है और प्रविष्टियों के बीच अद्वितीय विशेषता को वापस कर देना चाहिए।
प्लगइन्स एलिमेंट तब एक एकल उपयोग टैग लागू करता है। हम केवल एक संपत्ति को परिभाषित करते हैं: असेंबलीनाम, जिसे असेंबली-एट्रिब्यूट में मैप किया गया है।
मैं पूरी तरह से यह सब समझने के लिए दावा नहीं करते हैं (विशेष रूप से ConfigurationElementCollection और यह विभिन्न BaseAdd, BaseGet आदि गुण वास्तव में यहाँ का पता लगाया है नहीं कर रहे हैं), लेकिन मैं दावा कर सकते हैं कि इस काम करता है :)
इसके अलावा, यह किसी भी विशेषताओं का उपयोग नहीं करता है। मुझे गुणों से नफरत है - सौभाग्य से, इन सभी विशेषताओं को उचित कोड में परिवर्तित किया जा सकता है। आप एक या दूसरे (या दोनों) का उपयोग कर सकते हैं।
यदि आप चाहें तो System.Web.Configuration.CustomErrorsSection और अन्य का उपयोग कर देखें या तो परावर्तक या डिबगिंग सर्वर। यह देखते हुए कि माइक्रोसॉफ्ट इन्हें कैसे लागू करता है (आईएमएचओ बल्कि जटिल) वर्ग अच्छी अंतर्दृष्टि देता है। –
एमएसडीएन दस्तावेज़ों से बेहतर! thanx – Aliostad
एमएसडीएन की तुलना में निश्चित रूप से बेहतर है। बहुत धन्यवाद! – Sergejs