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"];}
}