상속: IConfigurationSectionHandler
예제 #1
0
		public static IDictionary GetConfig(Assembly assembly)
		{
			string url = assembly.CodeBase + ".config";
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(new XmlTextReader(url));
			XmlNodeList elementsByTagName = xmlDocument.GetElementsByTagName("assemblySettings");
			foreach (XmlNode xmlNode in elementsByTagName)
			{
				if (xmlNode.LocalName == "assemblySettings")
				{
					DictionarySectionHandler dictionarySectionHandler = new DictionarySectionHandler();
					return (IDictionary)dictionarySectionHandler.Create(null, null, xmlNode);
				}
			}
			return null;
		}
예제 #2
0
        //----------------------------------------------------------------
        // <summary>
        /// method to open and parse the config
        /// file for the provided assembly
        /// </summary>
        /// <param name="asm">Assembly's config file to parse</param>
        /// <returns></returns>
        public IDictionary ReadConfig(Assembly asm)
        {
            try
            {
                //string to hold the name of the
                //config file for the assembly
                string assemblyName =  asm.CodeBase;
                int idx = assemblyName.IndexOf('.');
                string cfgFile = assemblyName.Substring(0, idx) + ".config";

                //create a new XML Document
                mDoc = new XmlDocument();
                //load an XML document by using the
                //XMLTextReader class of the XML Namespace
                //yo open the sfgFile
                mDoc.Load(new XmlTextReader(cfgFile));
                //retrieve a list of nodes in the document
                XmlNodeList nodes = mDoc.GetElementsByTagName(mNodeName);
                //now we need to loop through all the
                //nodes in the XML document
                foreach (XmlNode node in nodes)
                {
                    //now check to see if the name of the node
                    //in this iteration is the same as our global
                    //nodeName variable
                    if (node.LocalName == mNodeName)
                    {
                        //since they match we need to use the
                        //DictionarySectionHandler and create a
                        //new handler and add it to the collection
                        DictionarySectionHandler handler = new DictionarySectionHandler();
                        object o = handler.Create(null, null, node);
                        //rerutn the new handler
                        return (IDictionary)o;
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }

            return (null);
        }
예제 #3
0
        public static IDictionary GetConfig(Assembly asm)
        {
            // Open and parse configuration file for specified
            // assembly, returning collection to caller for future
            // use outside of this class.
            string cfgFile = asm.CodeBase + ".config";
            try
            {
                const string nodeName = "assemblySettings";
                
                XmlDocument doc = new XmlDocument();
                //if (File.Exists(cfgFile))
                //{
                    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);
                        }
                    }
               // }
               // else
               // {
               //     _log.Warn("Assembly configuration file not found: " + cfgFile);
               // }
            }
            catch (FileNotFoundException)
            {
                _log.Warn("Assembly configuration file not found: " + cfgFile);
            }
            catch (Exception e)
            {
                _log.Warn("Failed to load .config file: " + cfgFile, e);
            }

            return null;
        }
예제 #4
0
		public virtual object Create (object parent, object configContext, XmlNode section) {
			if (section.Attributes != null && section.Attributes.Count != 0)
				ThrowException ("Unrecognized attribute", section);

			ArrayList providers = new ArrayList();
			
			XmlNodeList providerElements = section.SelectNodes ("provider");
			foreach (XmlNode child in providerElements) {
				XmlNodeType ntype = child.NodeType;
				if (ntype == XmlNodeType.Whitespace || ntype == XmlNodeType.Comment)
					continue;

				if (ntype != XmlNodeType.Element)
					ThrowException ("Only elements allowed", child);

				object parentProvider = null;
				XmlAttribute extends = child.Attributes["parent"];
				if (extends != null) {
					string super = extends.Value;
					string sectionName = super.Substring(0, super.IndexOf('#'));
					string id = super.Substring(sectionName.Length+1);
					IList supers = (IList) ConfigurationSettings.GetConfig(sectionName);
					for (int i = 0; i < supers.Count; i++) {
						Hashtable ht = (Hashtable)supers[i];
						string curId = (string)ht["id"];
						if (String.CompareOrdinal(id,curId) == 0) {
							parentProvider = ht;
							break;
						}
					}
					child.Attributes.Remove(extends);
				}

				DictionarySectionHandler handler = new DictionarySectionHandler();
				Hashtable col = (Hashtable) handler.Create (parentProvider, null, child);
				providers.Add(col);
			}	
			if (parent != null && parent is IList) {
				IList parentList = (IList)parent;
				for (int i = 0; i < parentList.Count; i++) {
					Hashtable htParent = (Hashtable)parentList[i];
					string parentId = (string)htParent["id"];
					bool overriden = false;
					for (int y = 0; y < providers.Count; y++) {
						Hashtable htMe = (Hashtable)providers[y];
						string myId = (string)htMe["id"];
						if (String.CompareOrdinal(parentId,myId) == 0) {
							overriden = true;
							foreach (object key in htParent.Keys)
								if (!htMe.ContainsKey(key))
									htMe[key] = htParent[key];
							break;
						}
					}

					if (!overriden)
						providers.Add(htParent);
				}
			}

			return providers;
		}
예제 #5
0
        public IDictionary GetConfig(string NameValue)
        {
            XmlNodeList Section = XmlConfiguration.GetElementsByTagName(NameValue);
            foreach (XmlNode Node in Section)
            {
                if (Node.LocalName == NameValue)
                {
                    DictionarySectionHandler NodeHandler = new DictionarySectionHandler();
                    return (IDictionary)NodeHandler.Create(null, null, Node);
                }
            }

            return null;
        }