ConfigurationSettings.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //
  2. // System.Configuration.ConfigurationSettings.cs
  3. //
  4. // Author:
  5. // Christopher Podurgiel ([email protected])
  6. //
  7. // C) Christopher Podurgiel
  8. //
  9. using System;
  10. using System.Collections.Specialized;
  11. using System.Reflection;
  12. using System.Xml;
  13. using System.Xml.XPath;
  14. namespace System.Configuration
  15. {
  16. /// <summary>
  17. /// Component class.
  18. /// </summary>
  19. /// <remarks>
  20. /// Longer description
  21. /// </remarks>
  22. public sealed class ConfigurationSettings
  23. {
  24. private static string applicationConfigFileName;
  25. /// <summary>
  26. /// ConfigurationSettings Constructor.
  27. /// </summary>
  28. private ConfigurationSettings ()
  29. {
  30. }
  31. /// <summary>
  32. /// Returns configuration settings for a user-defined configuration section.
  33. /// </summary>
  34. /// <param name="sectionName">The name of the configuration section that configuration settings are read from.</param>
  35. /// <returns></returns>
  36. public static object GetConfig(string sectionName)
  37. {
  38. // NOTE: We can only use Assembly.GetCallingAssembly() if we're called
  39. // directly from user code.
  40. return GetConfig (Assembly.GetCallingAssembly (), sectionName);
  41. }
  42. private static object GetConfig(Assembly assembly, string sectionName)
  43. {
  44. //Get the full path to the Applicaton Configuration File.
  45. applicationConfigFileName = assembly.Location + ".config";
  46. //Create an instance of an XML Document.
  47. XmlDocument ConfigurationDocument = new XmlDocument();
  48. //Try to load the XML Document.
  49. try
  50. {
  51. ConfigurationDocument.Load(applicationConfigFileName);
  52. }
  53. catch(XmlException e)
  54. {
  55. //Error loading the XML Document. Throw a ConfigurationException.
  56. throw(new ConfigurationException(e.Message, applicationConfigFileName, e.LineNumber));
  57. }
  58. string sectionHandlerName = GetSectionHandlerType(ConfigurationDocument, sectionName);
  59. XmlNode sectionNode = ConfigurationDocument.SelectSingleNode("/configuration/" + sectionName);
  60. //If the specified sectionName is not found, then sectionNode will be null. When calling objNVSHandler.Create(),
  61. //sectionNode cannot be null.
  62. if(sectionNode == null)
  63. {
  64. return null;
  65. }
  66. //Create a new SectionHandler
  67. //According to the Docs provided by Microsoft, the user can create their own configuration sections, and create a custom
  68. //handler class for it. The user would specify the class and its assebly in the <configSections> section. These would be
  69. //seperated by a comma.
  70. string sectionHandlerClassName = sectionHandlerName;
  71. string sectionHandlerAssemblyName = "System";
  72. //Split the SectionHandler Class Name from the Assembly Name (if provided).
  73. string[] sectionHandlerArray = sectionHandlerName.Split(new char[]{','}, 2);
  74. if(sectionHandlerArray.Length == 2)
  75. {
  76. sectionHandlerClassName = sectionHandlerArray[0];
  77. sectionHandlerAssemblyName = sectionHandlerArray[1];
  78. }
  79. // Load the assembly to use.
  80. Assembly assem = Assembly.Load(sectionHandlerAssemblyName);
  81. //Get the class type.
  82. Type handlerObjectType = assem.GetType(sectionHandlerClassName);
  83. //Get a reference to the method "Create"
  84. MethodInfo createMethod = handlerObjectType.GetMethod("Create");
  85. //Create an Instance of this SectionHandler.
  86. Object objSectionHandler = Activator.CreateInstance(handlerObjectType);
  87. //define the arguments to be passed to the "Create" Method.
  88. Object[] args = new Object[3];
  89. args[0] = null;
  90. args[1] = null;
  91. args[2] = sectionNode;
  92. object sectionHandlerCollection = createMethod.Invoke(objSectionHandler, args);
  93. //Return the collection
  94. return sectionHandlerCollection;
  95. }
  96. /// <summary>
  97. /// Gets the name of the SectionHandler Class that will handle this section.
  98. /// </summary>
  99. /// <param name="xmlDoc">An xml Configuration Document.</param>
  100. /// <param name="sectionName">The name of the configuration section that configuration settings are read from.</param>
  101. /// <returns>The name of the Handler Object for this configuration section, including the name if its Assembly.</returns>
  102. [MonoTODO]
  103. private static string GetSectionHandlerType(XmlDocument xmlDoc, string sectionName)
  104. {
  105. //TODO: This method does not account for sectionGroups yet.
  106. string handlerName = null;
  107. //<appSettings> is a predefined configuration section. It does not have a definition
  108. // in the <configSections> section, and will always be handled by the NameValueSectionHandler.
  109. if(sectionName == "appSettings")
  110. {
  111. handlerName = "System.Configuration.NameValueSectionHandler";
  112. }
  113. else
  114. {
  115. string[] sectionPathArray = sectionName.Split(new char[]{'/'});
  116. //Build an XPath statement.
  117. string xpathStatement = "/configuration/configSections";
  118. for (int i=0; i < sectionPathArray.Length; i++)
  119. {
  120. if(i < sectionPathArray.Length - 1)
  121. {
  122. xpathStatement = xpathStatement + "/sectionGroup[@name='" + sectionPathArray[i] + "']";
  123. }
  124. else
  125. {
  126. xpathStatement = xpathStatement + "/section[@name='" + sectionPathArray[i] + "']";
  127. }
  128. }
  129. //Get all of the <section> node using the xpath statement.
  130. XmlNode sectionNode = xmlDoc.SelectSingleNode(xpathStatement);
  131. // if this section isn't found, then there was something wrong with the config document,
  132. // or the sectionName didn't have a proper definition.
  133. if(sectionNode == null)
  134. {
  135. throw (new ConfigurationException("Unrecognized element."));
  136. }
  137. handlerName = sectionNode.Attributes["type"].Value;
  138. }
  139. //Return the name of the handler.
  140. return handlerName;
  141. }
  142. /// <summary>
  143. /// Get the Application Configuration Settings.
  144. /// </summary>
  145. public static NameValueCollection AppSettings
  146. {
  147. get
  148. {
  149. //Get the Configuration Settings for the "appSettings" section.
  150. NameValueCollection appSettings = (NameValueCollection)GetConfig(
  151. Assembly.GetCallingAssembly (), "appSettings");
  152. return appSettings;
  153. }
  154. }
  155. }
  156. }