![]() |
Beginner
License: The Code Project Open License (CPOL)
Writing a complex custom configuration sectionBy aftabahmed.netTechnorati Tags: C#,.Net FrameworkHere i posted another entry, showing implementation of a simple configuration section, now consider following listing<customSection> <elements> <add name=”FirstAssembly.FirstType” assembly=”FirstAssembly” shoul |
C#, Windows, .NET (.NET2.0, .NET3.0, .NET3.5), Dev
|
||||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
Here i posted another entry, showing implementation of a simple configuration section, now consider following listing
<customSection> <elements> <add name=”FirstAssembly.FirstType” assembly=”FirstAssembly” shouldrun=”true”></add> <add name=”SecondAssembly.SecondType” assembly=”SecondAssembly” shouldrun=”true”></add> </elements> </customSection>
here we have “n” number of elements in the custom configuration section. To handle this kind of scenario, System.Configuration namespace provides you with ConfigurationPropertyCollection class and ConfigurationPropertyCollectionAttribute class. Your can use ConfigurationPropertyCollection to programmatically write you configuration section and ConfigurationPropertyCollectionAttribute can be used to do the same job declaratively.
There are following steps involved in creating a custom configuration section handler with multiple entries.
Here is the listing for CustomeElement Class
public class CustomElement : ConfigurationElement { public CustomElement() { } [ConfigurationProperty("name", IsRequired = true)] public string Name { get { return (string)this["name"]; } set { this["name"] = value; } } [ConfigurationProperty("assembly", IsRequired = true)] public string Assembly { get { return (string)this["assembly"]; } set { this["assembly"] = value; } } [ConfigurationProperty("shouldrun", IsRequired = true)] public bool ShouldRun { get { return (bool)this["shouldrun"]; } set { this["shouldrun"] = value; } } }
and here is the listing for CustomElementCollection Class, its implementation is similar to any other Collection class,
public class CustomeElementCollection : ConfigurationElementCollection { public CustomeElementCollection() { CustomElement myElement = (CustomElement)CreateNewElement(); Add(myElement); } public void Add(CustomElement customElement) { BaseAdd(customElement); } protected override void BaseAdd(ConfigurationElement element) { base.BaseAdd(element, false); } public override ConfigurationElementCollectionType CollectionType { get { return ConfigurationElementCollectionType.AddRemoveClearMap; } } protected override ConfigurationElement CreateNewElement() { return new CustomElement(); } protected override object GetElementKey(ConfigurationElement element) { return ((CustomElement)element).Name; } public CustomElement this[int Index] { get { return (CustomElement)BaseGet(Index); } set { if (BaseGet(Index) != null) { BaseRemoveAt(Index); } BaseAdd(Index,value); } } new public CustomElement this[string Name] { get { return (CustomElement)BaseGet(Name); } } public int indexof(CustomElement element) { return BaseIndexOf(element); } public void Remove(CustomElement url) { if (BaseIndexOf(url) >= 0) BaseRemove(url.Name); } public void RemoveAt(int index) { BaseRemoveAt(index); } public void Remove(string name) { BaseRemove(name); } public void Clear() { BaseClear(); } }
and Finally, here is the listing for CustomSection Class,
class CustomSection:ConfigurationSection { CustomElement element; public CustomSection() { element = new CustomElement(); } [ConfigurationProperty("elements", IsDefaultCollection = false)] [ConfigurationCollection(typeof(CustomeElementCollection),AddItemName="add", ClearItemsName="clear", RemoveItemName="remove")] public CustomeElementCollection Elements { get { return (CustomeElementCollection)base["elements"]; } } }
Next add following settings to your configuration file
<configSections> <section name=”customSection” type=”ConfigurationDemo.CustomSection, ConfigurationDemo” /> </configSections> <customSection> <elements> <add name=”FirstAssembly.FirstType” assembly=”FirstAssembly” shouldrun=”true”></add> <add name=”SecondAssembly.SecondType” assembly=”SecondAssembly” shouldrun=”true”></add> </elements> </customSection>
in the end put this custom section to use like this,
CustomSection myCustomSection = (CustomSection)ConfigurationManager.GetSection(”customSection2″);
foreach (CustomElement element in mySecondSectionHandler.Elements)
{
if (element.Name != null && element.Name != “”)
{
string name = element.Name;
string assembly = element.Assembly;
bool shouldrun = element.ShouldRun;
}
}
| You must Sign In to use this message board. | |||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
|
|||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 2 Jul 2009 Editor: Sean Ewington |
Copyright 2009 by aftabahmed.net Everything else Copyright © CodeProject, 1999-2010 Web11 | Advertise on the Code Project |