5

I have a working custom configuration section. However, it is a pain to get at my data via a ConfigurationElementCollection but when I try to implement my property as an IEnumerable, it fails with the error:

ConfigurationErrorsException was unhandled "Property 'contacts' is not a ConfigurationElement."

Here is the code causing the failure:

[ConfigurationProperty("contacts", IsDefaultCollection = false)]
public IEnumerable<string> Contacts
{
    get { return ((ContactCollection)base["contacts"]).Cast<ContactElement>().Select(x => x.Address); }
}

However, if I change it to this:

[ConfigurationProperty("contacts", IsDefaultCollection = false)]
public ContactCollection Contacts
{
    get { return ((ContactCollection)base["contacts"]); }
}

Everything works fine. This answer makes it sound like this is just something Microsoft decided was not allowed and so I can't have any properties of types other than ConfigurationElement. Is this really the case? How can I implement my property as an IEnumerable<string>?

In case it matters, I'm trying to store emails and I'd like to have an element for each one since there may be a number of them, we may want to store more information on each contact in the future, and I think a single, comma-separated list might get ugly. For example, something like:

<emergency>
    <contact address="[email protected]" />
    <contact address="[email protected]" />
</emergency>

Or

<emergency>
    <contact>[email protected]</contact>
    <contact>[email protected]</contact>
</emergency>

Thanks!

2

2 Answers 2

5
+50

Any harm in providing two methods? The first to fulfill the Microsoft requirement and the second to fulfill your own requirements.

    public IEnumerable<string> Contacts
    {
        get
        {
            return ContactCollection.Cast<ContactElement>().Select(x => x.Address);     
        }
    }

    [ConfigurationProperty("contacts", IsDefaultCollection = false)]
    public ContactCollection ContactCollection
    {
        get { return ((ContactCollection)base["contacts"]); }
    }
4
  • I would like to avoid doing this but I don't know why someone downvoted you. If this is the only solution, I'll accept it in the future. Also, thanks for your first answer after five years of being here! :)
    – sirdank
    Jul 15, 2016 at 19:22
  • 1
    Note you can even declare the ContactCollection as private wich is interesting if you want to mask the property from callers & intellisense. Jul 17, 2016 at 6:20
  • Lol... my first answer and I was immediately downvoted! I was wise to be afraid ;)
    – Will
    Jul 18, 2016 at 15:48
  • Although the need for this solution seems silly to me, with the private modifier it works about as well as I could hope. Thanks!
    – sirdank
    Jul 22, 2016 at 12:26
1

One 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.