Click here to Skip to main content
15,885,216 members
Articles / Programming Languages / C#

How to make AppSettings work with multiple values for a key

Rate me:
Please Sign up or sign in to vote.
4.37/5 (22 votes)
6 Jul 20033 min read 267.6K   3K   44  
Fixing NameValueSectionHandler using reflection and using it seamlessly.
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.Globalization;
using System.Reflection;
using System.Xml;

namespace DigBang.Configuration
{
	public class NameValueMultipleSectionHandler: IConfigurationSectionHandler
	{
		static Type readOnlyNameValueCollectionType = null;
		static ConstructorInfo readOnlyNameValueCollectionConstructor1 = null;
		static ConstructorInfo readOnlyNameValueCollectionConstructor2 = null;

		static NameValueMultipleSectionHandler()
		{
			readOnlyNameValueCollectionType = typeof(NameValueCollection).
				Assembly.GetType("System.Configuration.ReadOnlyNameValueCollection");
			if (readOnlyNameValueCollectionType != null)
			{
				readOnlyNameValueCollectionConstructor1 = 
					readOnlyNameValueCollectionType.GetConstructor(
					new Type[] {readOnlyNameValueCollectionType});

				readOnlyNameValueCollectionConstructor2 = 
					readOnlyNameValueCollectionType.GetConstructor(
					new Type[] {typeof(IHashCodeProvider), typeof(IComparer)});
			}
		}

		static NameValueCollection CreateCollection(object parent)
		{
			if (parent == null)
			{
				return 
					(NameValueCollection)readOnlyNameValueCollectionConstructor2.Invoke(
					new object[] {
									 new CaseInsensitiveHashCodeProvider(
									 CultureInfo.InvariantCulture),
									 new CaseInsensitiveComparer(
									 CultureInfo.InvariantCulture)});
			}
			else
			{
				return 
					(NameValueCollection)readOnlyNameValueCollectionConstructor1.Invoke(
					new object[] {parent});

			}
		}

		static NameValueCollection GetConfig(string sectionName)
		{
			return (NameValueCollection)ConfigurationSettings.GetConfig(sectionName);
		}

		public object Create(object parent, object context, XmlNode section)
		{
			NameValueCollection collection = null;

			if (readOnlyNameValueCollectionType != null)
			{
				collection = CreateCollection(parent);
				foreach (XmlNode xmlNode in section.ChildNodes)
				{
					if (xmlNode.NodeType == XmlNodeType.Element)
					{
						switch (xmlNode.Name)
						{
							case "add":
								collection.Add(xmlNode.Attributes["key"].Value,
									xmlNode.Attributes["value"].Value);
								break;
							case "remove":
								collection.Remove(xmlNode.Attributes["key"].Value);
								break;
							case "clear":
								collection.Clear();
								break;
						}
					}
				}
			}
			return collection;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions