namespace Zerotrilogy.ApplicationBlocks.Framework
{
        using System;
        using System.Reflection;
        using System.Collections;
        using System.Collections.Specialized;
        using System.Configuration;
        using System.Xml;

public class ClassConfig
        {
                private static IDictionary settings;
                private static string configRoot;
                private static Assembly assembly;

                /// <summary>
                /// Default constructor, will retrieve configuration settings for the calling assembly
                /// </summary>
                public ClassConfig() : this(Assembly.GetCallingAssembly()) { }

                /// <summary>
                /// Overloaded constructor, will retrieve configuration settings for the specified assembly
                /// </summary>
                /// <param name="asm">The assembly to retrieve configuration settings for</param>
                public ClassConfig(Assembly asm) : this(asm, "appSettings")     { }

                /// <summary>
                /// Overloaded constructor, will retrieve configuration settings for the specified assembly using the specified root element in the configuration file
                /// </summary>
                /// <param name="asm">The assembly to retrieve configuration settings for</param>
                /// <param name="configRootElement">The root element of the configuration file</param>
                public ClassConfig(Assembly asm, string configRootElement)
                {
                        assembly = asm;
                        settings = GetConfig(asm);
                        configRoot = configRootElement;
                }

                /// <summary>
                /// Gets and sets the root configuration element in the calling assembly's configuration file
                /// </summary>
                public string ConfigurationRoot
                {
                        get { return configRoot; }
                        set { configRoot = value; }
                }

                /// <summary>
                /// Indexer used to retrieve a specific key value
                /// </summary>
                /// <remarks>
                /// Used by external classes to retrieve configuration settings
                /// </remarks>
                /// the <paramref name="key" /> takes the name of the configuration setting/>
                public string this[string key]
                {
                        get
                        {
                                string settingValue = null;
                                settings = GetConfig(assembly);

                                if( settings != null )
                                {
                                        settingValue = settings[key] as string;
                                }

                                return(settingValue == null ? "" : settingValue);
                        }
                }

                /// <summary>
                /// Indexer used to retrieve a specific key value for the specified assembly
                /// </summary>
                /// <remarks>
                /// Used by external classes to retrieve configuration settings for an external assembly
                /// </remarks>
                /// the <paramref name="key" /> takes the name of the configuration setting/>
                /// the <paramref name="Assembly" /> takes the assembly to get the configuration setting for/>
                public string this[string key, Assembly assem]
                {
                        get
                        {
                                string settingValue = null;
                                settings = GetConfig(assem);

                                if (settings != null)
                                {
                                        settingValue = settings[key] as string;
                                }

                                return (settingValue == null ? "" : settingValue);
                        }
                }

                /// <summary>
                /// Gets the configuration settings for the calling assembly
                /// </summary>
                /// <returns>An object of type IDictionary containing a Kay Value pair</returns>
                public static IDictionary GetConfig()
                {
                        return GetConfig(assembly);
                }

                /// <summary>
                /// Gets the configuration settings for the specified assembly
                /// </summary>
                /// <param name="asm">The assembly to get the configuration settings for</param>
                /// <returns>An object of type IDictionary containing a Kay Value pair</returns>
                public static IDictionary GetConfig( Assembly asm )
                {
                        string cfgFile = string.Empty;
                        string nodeName = string.Empty;
                        // Open and parse configuration file for specified
                        // assembly, returning collection to caller for future
                        // use outside of this class.
                        try
                        {
                                cfgFile = asm.CodeBase + ".config";
                                nodeName = configRoot;
                        }
                }
                        XmlDocument doc = new XmlDocument();
                doc.Load(new XmlTextReader(cfgFile));
   
                XmlNodeList nodes = doc.GetElementsByTagName(nodeName);
                foreach( XmlNode node in nodes )
                {
                if( node.LocalName == nodeName )
                {
                        DictionarySectionHandler handler = new DictionarySectionHandler();
                        return (IDictionary)handler.Create(null, null, node);
                }
        }
        }
        catch { }

        return(null);
                }
        }
}