Custom config section with non-ConfigurationElement properties
See the question and my original answer on StackOverflowOne solution if you only need a list of string, is to declare a comma-separated (or any separator) list of values like this:
[ConfigurationProperty("contacts")]
[TypeConverter(typeof(StringSplitConverter))]
public IEnumerable<string> Contacts
{
get
{
return (IEnumerable<string>)base["contacts"];
}
}
With this TypeConverter class:
public class StringSplitConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
return string.Format("{0}", value).Split(',');
}
}
Your .config file would then simply be declared like this:
<configuration>
...
<mySection contacts="bill,joe" />
...
</configuration>
Note this doesn't work for collections, when you always need to explicitely declare a property, like in Will's answer.