Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does .net custom config section elements have to have keys?

Concider the following config section:

<PluginSection>
    <Plugins>
       <Plugin name="Plug1">
          <add MessageType="1" MessageSubType="1" Ringtone="chime.wav" Vibrate="1000:0:1"/>
          <add MessageType="1" MessageSubType="2" Ringtone="chime2.wav" Vibrate="1000:0:1"/>
       </Plugin>
       <Plugin name="Plug2">
          <add MessageType="1" MessageSubType="1" Ringtone="chime.wav"/>
          <add MessageType="1" MessageSubType="2" Ringtone="chime2.wav"/>
          <add MessageType="2" Ringtone="chime3.wav"/>
       </Plugin>
    </Plugins>
 </PluginSection>

I've implemented the parsing of this as a c# IConfigSectionHandler. Now I understand that this method is deprecated, and that I should use ConfigurationSection, ConfigurationElements and ConfigurationElementCollections. I have no problem understanding the examples of this on the web (msdn and SO). But all the examples I've seen so far have used one of the properties as a key. My elements are unique as a combination of the Plugin name, MessageType and MessageSubType. The MessageSubType is also optional. Could i parse a config section that looks like this using the recommended classes, or do I have to alter my configuration to fit the regime of the ConfigurationClasses by e.g. adding a "dummy" key?

like image 674
Jan Petter Jetmundsen Avatar asked Jun 26 '13 06:06

Jan Petter Jetmundsen


1 Answers

No.

But to avoid keys you need to do more work.

The concrete type KeyValueConfigurationCollection allows easy creation of a configuration collection by setting some properties.

To create a more customised collection requires either extending the abstract ConfigurationElementCollection (but this will still be based on the add/remove/clear model as used by <appSettings>. but allowing the element names to be configured but this is still based on ahaving a key value for each member of the collection (this is determined by an override of GetElementKey so does not need to be directly included in the XML).

Alternatively you can create you own, completely custom configuration collection by extending ConfigurationElement, but you'll need to do all the work of parsing the child elements yourself (remember ConfigurationElementCollection is itself a child class of ConfigurationElement).

like image 194
Richard Avatar answered Oct 05 '22 00:10

Richard