Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include simple collections in ConfigurationSection

Is there a way for me to include a simple array of strings, or List<string> on my custom subclass of ConfigurationSection? (Or an array or generic list of simple data objects, for that matter?)

I'm becoming familiar with the new (and VERY verbose) ConfigurationSection, ConfigurationElement, and ConfigurationElementCollection classes, but I'm by no means an expert yet.

It seems like ConfigurationSection should handle simple collections/lists on its own, without me having to create a custom ConfigurationElementCollection subclass for each and every one. But I haven't found any reference to this ability online.

Edit: accepting Dan's response as the answer, since it's probably the closest thing I'm going to get to the "old style" configSections. I always found it easy, flexible, and elegant that any XmlSerializable object could easily become a configSection. I'm sure the new framework is more powerful; however it's sad that it is SO cumbersome for simple configuration contructs, that we're reduced to going back to String.Split().

like image 722
mikemanne Avatar asked Apr 23 '10 13:04

mikemanne


3 Answers

Application Settings Architecture

http://msdn.microsoft.com/en-us/library/8eyb2ct1.aspx

Ok, an old post, but I remembered it when I came across a similar situation:

...

If you go to Project / Project Properties (in VS2008 or VS2010). There is a "Settings" tab.

If you add a new value....

One of the types is called: System.Collections.Specialized.StringCollection

Give it a name (I used "FavoriteColors").

Set the type (as instructed above).

Set the value(s).

The "String Collection Editor" says "Enter the strings in the collection (one per line)".

I entered:

Red

Yellow

Black

White

This will add some xml to your app.config file.

    <setting name="FavoriteColors" serializeAs="Xml">
        <value>
            <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                <string>red</string>
                <string>yellow</string>
                <string>black</string>
                <string>white</string>
            </ArrayOfString>
        </value>
    </setting>

(You'll be better off going through the steps rather than pasting the xml above, because (for conciseness) I did not add all the xml to this post that is generated.

You should be able to "get at" the values via code like this:

private void ShowMyFavoriteColors()
{
            Properties.Settings.Default.FavoriteColors.Cast<string>().ToList().ForEach(myfavcolor =>
            {
                string temp = myfavcolor;
            });
}

Note, the steps above will produce the below C# code (auto code created for you.... it is not code you create) but the code looks like this:

        [global::System.Configuration.ApplicationScopedSettingAttribute()]
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.Configuration.DefaultSettingValueAttribute(@"<?xml version=""1.0"" encoding=""utf-16""?>
<ArrayOfString xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
  <string>red</string>
  <string>yellow</string>
  <string>black</string>
  <string>white</string>
</ArrayOfString>")]
        public global::System.Collections.Specialized.StringCollection FavoriteColors {
            get {
                return ((global::System.Collections.Specialized.StringCollection)(this["FavoriteColors"]));
            }
        }
    }
}
like image 102
granadaCoder Avatar answered Nov 14 '22 17:11

granadaCoder


OK, you asked for simple. Well, the simplest way to store a series of strings would be to use a delimited list (say, comma separated). That way you can store it in just one element (say in appSettings).

<add key="weekDays" value="Monday,Tuesday,Wednesday,Thursday,Friday"/>

Of course, this has drawbacks but in most cases works well for a simple list. You can then use String.Split() to convert it back to an array/list.

Otherwise you are back to ConfigurationSection elements which, I agree, are very verbose and clumsy to work with. But I don't know of any other way, I'm afraid (but am happy to be proved wrong!).

like image 36
Dan Diplo Avatar answered Nov 14 '22 17:11

Dan Diplo


I know that the question has been answered long time ago... but in my 'ConfigurationElement' classes, for string collection, I usually do the following:

[ConfigurationProperty("myStringCollectionProperty", DefaultValue = "")]
[TypeConverter(typeof(CommaDelimitedStringCollectionConverter))]
public StringCollection MyStringCollectionProperty
{
    get { return (StringCollection)this["myStringCollectionProperty"]; }
    set { this["myStringCollectionProperty"] = value; }
 }

And you can get a string list from this property with

List<string> myStrings = config.MyStringCollectionProperty.Cast<string>.ToList()
like image 4
Thierry Avatar answered Nov 14 '22 18:11

Thierry