Welcome to RioterDeckers' HeadQuarter Sign in | Join | Help

.Net 2.0 System.Configuration store parameters - Part II of IV

II. Store a collection of elements
<SimpleDataList>
  <Datas>
    <add name="D_1" value="data1 of the elementCollection"/>
    <add name="D_2" value="data2 of the elementCollection"/>
    <add name="D_3" value="data3 of the elementCollection"/>
  </Datas>
</SimpleDataList>
You have to add to a configurationSection, a configurationElementCollection and you must describe the ConfigurationElement used by the collection.
 
Step 1 the section
As we did previously, create a Section that derived from ConfigurationSection
Add a property that will return the configurationElementCollection defined above, this property must have an attribute of type ConfigurationProperty with the following parameters.
[ConfigurationProperty("Datas", IsDefaultCollection = true, Options = ConfigurationPropertyOptions.None)]
 
Step 2 the elementCollection
Define the collection class by deriving from ConfigurationElementCollection override the CreateNewElement() to return the new TypedConfigurationElement. Override the GetElementKey(ConfigurationElement element) to return a specific TypedConfigurationElement by the property identified within the TypedConfigurationElement
 as the collection key.
protected override object GetElementKey(ConfigurationElement element)
{
  return ((DataElement)element).Name;
}
Note: you must also add a public indexer in order to be able to fetch the configurationElementCollection
public DataElement this[string name]
{
  get
  {
    return (DataElement)base.BaseGet(name);
  }
}
 
Step3 the element
Create the element by deriving from the ConfigurationElement add the property as we already did in the first sample but don’t forget to define an attribute as the collectionElement key.
[ConfigurationProperty("name", IsKey=true)]
public string Name
{
  get{return (string) base["name"];}
}
 
Published Friday, December 09, 2005 7:13 AM by WoZoI

Comments

# [System.Configuration] Support of generics.

We didn't celebrate
Christmas yet, but I know already that one of letters to Santa Claus arrived at...
Monday, June 05, 2006 4:20 PM by Nezdeboeuf
Anonymous comments are disabled