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;
///
/// Default constructor, will retrieve configuration settings for the calling assembly
///
public ClassConfig() : this(Assembly.GetCallingAssembly()) { }
///
/// Overloaded constructor, will retrieve configuration settings for the specified assembly
///
/// The assembly to retrieve configuration settings for
public ClassConfig(Assembly asm) : this(asm, "appSettings") { }
///
/// Overloaded constructor, will retrieve configuration settings for the specified assembly using the specified root element in the configuration file
///
/// The assembly to retrieve configuration settings for
/// The root element of the configuration file
public ClassConfig(Assembly asm, string configRootElement)
{
assembly = asm;
settings = GetConfig(asm);
configRoot = configRootElement;
}
///
/// Gets and sets the root configuration element in the calling assembly's configuration file
///
public string ConfigurationRoot
{
get { return configRoot; }
set { configRoot = value; }
}
///
/// Indexer used to retrieve a specific key value
///
///
/// Used by external classes to retrieve configuration settings
///
/// the 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);
}
}
///
/// Indexer used to retrieve a specific key value for the specified assembly
///
///
/// Used by external classes to retrieve configuration settings for an external assembly
///
/// the takes the name of the configuration setting/>
/// the 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);
}
}
///
/// Gets the configuration settings for the calling assembly
///
/// An object of type IDictionary containing a Kay Value pair
public static IDictionary GetConfig()
{
return GetConfig(assembly);
}
///
/// Gets the configuration settings for the specified assembly
///
/// The assembly to get the configuration settings for
/// An object of type IDictionary containing a Kay Value pair
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);
}
}
}